import { apiClient } from '@/lib/api-client';
import type {
  EmployeeByIdResponse,
  EmployeeFilters,
  EmployeeMutationPayload,
  EmployeesResponse
} from './types';

export async function getEmployees(filters: EmployeeFilters): Promise<EmployeesResponse> {
  const searchParams = new URLSearchParams();

  if (filters.page) searchParams.set('page', String(filters.page));
  if (filters.limit) searchParams.set('limit', String(filters.limit));
  if (filters.search) searchParams.set('search', filters.search);
  if (filters.statuses) searchParams.set('statuses', filters.statuses);
  if (filters.sort) searchParams.set('sort', filters.sort);

  return apiClient<EmployeesResponse>(`/employees?${searchParams.toString()}`);
}

export async function getEmployeeById(id: number): Promise<EmployeeByIdResponse> {
  return apiClient<EmployeeByIdResponse>(`/employees/${id}`);
}

export async function createEmployee(data: EmployeeMutationPayload) {
  return apiClient<EmployeeByIdResponse>('/employees', {
    method: 'POST',
    body: JSON.stringify(data)
  });
}

export async function updateEmployee(id: number, data: EmployeeMutationPayload) {
  return apiClient<EmployeeByIdResponse>(`/employees/${id}`, {
    method: 'PATCH',
    body: JSON.stringify(data)
  });
}

export async function deleteEmployee(id: number) {
  return apiClient<{ success: boolean; message: string }>(`/employees/${id}`, {
    method: 'DELETE'
  });
}
