// Leave requests (docs/13-BACKEND-HRM-API.md §5).
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import { LeaveRequest, LeaveRequestStatus } from "@/types/hrm"

export interface ListLeaveRequestsParams {
  page?: number
  pageSize?: number
  employeeId?: number
  status?: LeaveRequestStatus
}

export interface CreateLeaveRequestRequest {
  employeeId: number
  leaveTypeId: number
  startDate: string
  endDate: string
  reason?: string | null
}

export const leaveRequestsApi = {
  list(params: ListLeaveRequestsParams = {}): Promise<PagedResponse<LeaveRequest>> {
    return apiRequest<PagedResponse<LeaveRequest>>(`/leave-requests${buildQuery(params)}`)
  },
  get(leaveRequestId: number): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>(`/leave-requests/${leaveRequestId}`)
  },
  create(request: CreateLeaveRequestRequest): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>("/leave-requests", { method: "POST", body: request })
  },
  submit(leaveRequestId: number): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>(`/leave-requests/${leaveRequestId}/submit`, { method: "POST" })
  },
  approve(leaveRequestId: number): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>(`/leave-requests/${leaveRequestId}/approve`, { method: "POST" })
  },
  reject(leaveRequestId: number, reason: string): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>(`/leave-requests/${leaveRequestId}/reject`, { method: "POST", body: { reason } })
  },
  cancel(leaveRequestId: number): Promise<LeaveRequest> {
    return apiRequest<LeaveRequest>(`/leave-requests/${leaveRequestId}/cancel`, { method: "POST" })
  },
}
