import apiClient from './client'
import type { 
  Personnel, 
  PersonnelApprovalRequest,
  PaginatedResponse,
  PaginationParams 
} from '@/types'

export const personnelApi = {
  async getAll(params?: PaginationParams & { status?: string }): Promise<PaginatedResponse<Personnel>> {
    const response = await apiClient.get<PaginatedResponse<Personnel>>('/personnel', { params })
    return response.data
  },

  async getById(id: number): Promise<Personnel> {
    const response = await apiClient.get<Personnel>(`/personnel/${id}`)
    return response.data
  },

  async create(formData: FormData): Promise<Personnel> {
    const response = await apiClient.post<Personnel>('/personnel', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return response.data
  },

  async update(id: number, formData: FormData): Promise<Personnel> {
    const response = await apiClient.put<Personnel>(`/personnel/${id}`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    return response.data
  },

  async delete(id: number): Promise<void> {
    await apiClient.delete(`/personnel/${id}`)
  },

  async approve(data: PersonnelApprovalRequest): Promise<Personnel> {
    const response = await apiClient.post<Personnel>(`/personnel/${data.personnelId}/approve`)
    return response.data
  },

  async reject(data: PersonnelApprovalRequest): Promise<Personnel> {
    const response = await apiClient.post<Personnel>(`/personnel/${data.personnelId}/reject`, {
      approved: data.approved,
      comments: data.comments
    })
    return response.data
  },

  async getPendingApprovals(): Promise<Personnel[]> {
    const response = await apiClient.get<Personnel[]>('/personnel/pending')
    return response.data
  }
}
