import type {
	CreateUser,
	CreateUserProfile,
	UpdateUser,
	UpdateUserProfile,
	User,
	UserProfile,
	UserStats,
} from "@shared/schema";
import { apiRequest } from "@/lib/queryClient";

export interface LoginRequest {
	email: string;
	password: string;
}

export interface RegisterRequest extends CreateUser {
	password: string;
}

export interface AuthResponse {
	token: string;
	user: User;
}

export interface ChangePasswordRequest {
	currentPassword: string;
	newPassword: string;
}

/**
 * API Client for user-related operations
 * Provides type-safe methods for all user API endpoints
 */
export const userApiClient = {
	// Authentication
	async login(data: LoginRequest): Promise<AuthResponse> {
		return apiRequest<AuthResponse>("POST", "/api/auth/login", data);
	},

	async register(data: RegisterRequest): Promise<AuthResponse> {
		return apiRequest<AuthResponse>("POST", "/api/auth/register", data);
	},

	async logout(): Promise<void> {
		return apiRequest<void>("POST", "/api/auth/logout");
	},

	async refreshToken(): Promise<AuthResponse> {
		return apiRequest<AuthResponse>("POST", "/api/auth/refresh");
	},

	async forgotPassword(email: string): Promise<void> {
		return apiRequest<void>("POST", "/api/auth/forgot-password", { email });
	},

	async resetPassword(token: string, newPassword: string): Promise<void> {
		return apiRequest<void>("POST", "/api/auth/reset-password", {
			token,
			newPassword,
		});
	},

	async verifyEmail(token: string): Promise<void> {
		return apiRequest<void>("POST", "/api/auth/verify-email", { token });
	},

	async resendVerificationEmail(email: string): Promise<void> {
		return apiRequest<void>("POST", "/api/auth/resend-verification", {
			email,
		});
	},

	// User CRUD operations
	async getUser(userId: string): Promise<User> {
		return apiRequest<User>("GET", `/api/users/${userId}`);
	},

	async getUserByEmail(email: string): Promise<User> {
		return apiRequest<User>("GET", `/api/users/email/${email}`);
	},

	async getUserByUsername(username: string): Promise<User> {
		return apiRequest<User>("GET", `/api/users/username/${username}`);
	},

	async getUsers(params?: {
		limit?: number;
		offset?: number;
		role?: string;
	}): Promise<User[]> {
		const queryParams = new URLSearchParams();
		if (params?.limit) queryParams.append("limit", params.limit.toString());
		if (params?.offset) queryParams.append("offset", params.offset.toString());
		if (params?.role) queryParams.append("role", params.role);

		return apiRequest<User[]>("GET", `/api/users?${queryParams.toString()}`);
	},

	async getCurrentUser(): Promise<User> {
		return apiRequest<User>("GET", "/api/users/me");
	},

	async updateUser(userId: string, data: UpdateUser): Promise<User> {
		return apiRequest<User>("PUT", `/api/users/${userId}`, data);
	},

	async deleteUser(userId: string): Promise<void> {
		return apiRequest<void>("DELETE", `/api/users/${userId}`);
	},

	async changePassword(data: ChangePasswordRequest): Promise<void> {
		return apiRequest<void>("POST", "/api/users/change-password", data);
	},

	// User Profile operations
	async getUserProfile(userId: string): Promise<UserProfile> {
		return apiRequest<UserProfile>("GET", `/api/users/${userId}/profile`);
	},

	async createUserProfile(
		userId: string,
		data: CreateUserProfile,
	): Promise<UserProfile> {
		return apiRequest<UserProfile>(
			"POST",
			`/api/users/${userId}/profile`,
			data,
		);
	},

	async updateUserProfile(
		userId: string,
		data: UpdateUserProfile,
	): Promise<UserProfile> {
		return apiRequest<UserProfile>("PUT", `/api/users/${userId}/profile`, data);
	},

	// User Stats operations
	async getUserStats(userId: string): Promise<UserStats> {
		return apiRequest<UserStats>("GET", `/api/users/${userId}/stats`);
	},
};
