import { apiClient } from '@/lib/api-client';
import type {
  EmployeeDirectoryFilters,
  EmployeeDirectoryResponse,
  EmployeeTargetMutationPayload,
  EmployeeTargetResponse
} from './types';

export async function getEmployeeDirectory(
  filters: EmployeeDirectoryFilters
): Promise<EmployeeDirectoryResponse> {
  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.company) searchParams.set('company', filters.company);
  if (filters.department) searchParams.set('department', filters.department);
  if (filters.position) searchParams.set('position', filters.position);
  if (filters.status) searchParams.set('status', filters.status);
  if (filters.sort) searchParams.set('sort', filters.sort);

  return apiClient<EmployeeDirectoryResponse>(`/employee-directory?${searchParams.toString()}`);
}

export async function getEmployeeDirectoryTarget(id: number, year?: number) {
  const searchParams = new URLSearchParams();
  if (year) searchParams.set('year', String(year));

  return apiClient<EmployeeTargetResponse>(
    `/employee-directory/${id}/target${searchParams.size > 0 ? `?${searchParams.toString()}` : ''}`
  );
}

export async function upsertEmployeeDirectoryTarget(
  id: number,
  payload: EmployeeTargetMutationPayload
) {
  return apiClient<EmployeeTargetResponse>(`/employee-directory/${id}/target`, {
    method: 'PUT',
    body: JSON.stringify(payload)
  });
}
