import apiClient from '../../../shared/services/apiClient';

export interface User {
  id: number;
  full_name: string;
  email: string;
  phone_number?: string;
  avatar?: string;
  role: string;
  status?: string;
  created_at?: string;
  updated_at?: string;
}

export interface UserListResponse {
  success: boolean;
  status?: string;
  data: {
    users: User[];
    pagination?: {
      total: number;
      page: number;
      limit: number;
      totalPages: number;
    };
  };
  message?: string;
}

export interface CreateUserData {
  full_name: string;
  email: string;
  password: string;
  phone_number?: string;
  role: string;
  status?: string;
}

export interface UpdateUserData {
  full_name?: string;
  email?: string;
  phone_number?: string;
  role?: string;
  password?: string;
  status?: string;
}

export interface UserSearchParams {
  role?: string;
  status?: string;
  search?: string;
  page?: number;
  limit?: number;
}

export const getUsers = async (
  params: UserSearchParams = {}
): Promise<UserListResponse> => {
  const response = await apiClient.get<{ status?: string; success?: boolean; data?: { users?: User[]; pagination?: { total: number; page: number; limit: number; totalPages: number } }; message?: string }>('/users', { params });
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    status: data.status,
    data: {
      users: data.data?.users || [],
      pagination: data.data?.pagination,
    },
    message: data.message,
  };
};

export const getUserById = async (
  id: number
): Promise<{ success: boolean; data: { user: User } }> => {
  const response = await apiClient.get<{ status?: string; success?: boolean; data?: { user?: User }; message?: string }>(`/users/${id}`);
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  if (!data.data?.user) {
    throw new Error('User not found');
  }
  return {
    success: data.status === 'success' || data.success === true,
    data: { user: data.data.user },
  };
};

export const createUser = async (
  data: CreateUserData
): Promise<{ success: boolean; data: { user: User }; message: string }> => {
  const response = await apiClient.post<{ status?: string; success?: boolean; data?: { user?: User }; message?: string }>('/users', data);
  const responseData = response.data;
  // Handle both 'status: success' and 'success: true' formats
  if (!responseData.data?.user) {
    throw new Error('User creation failed');
  }
  return {
    success: responseData.status === 'success' || responseData.success === true,
    data: { user: responseData.data.user },
    message: responseData.message || 'User created successfully',
  };
};

export const updateUser = async (
  id: number,
  data: UpdateUserData
): Promise<{ success: boolean; data: { user: User }; message: string }> => {
  const response = await apiClient.put<{ status?: string; success?: boolean; data?: { user?: User }; message?: string }>(`/users/${id}`, data);
  const responseData = response.data;
  // Handle both 'status: success' and 'success: true' formats
  if (!responseData.data?.user) {
    throw new Error('User update failed');
  }
  return {
    success: responseData.status === 'success' || responseData.success === true,
    data: { user: responseData.data.user },
    message: responseData.message || 'User updated successfully',
  };
};

export const deleteUser = async (
  id: number
): Promise<{ success: boolean; message: string }> => {
  const response = await apiClient.delete<{ status?: string; success?: boolean; message?: string }>(`/users/${id}`);
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    message: data.message || 'User deleted successfully',
  };
};

export default {
  getUsers,
  getUserById,
  createUser,
  updateUser,
  deleteUser,
};
