import apiClient from './client'

export interface PendingIdentification {
  id: number
  face_id: number
  photo_id?: number | null
  user_id: number
  user_name?: string | null
  user_email: string
  first_name: string
  last_name: string
  middle_name?: string | null
  maiden_name?: string | null
  date_of_birth?: string | null
  status: string
  created_at: string
  updated_at: string
}

export interface PendingIdentificationsListResponse {
  items: PendingIdentification[]
  total: number
}

export interface ApproveDenyDecision {
  id: number
  decision: 'approve' | 'deny'
}

export interface ApproveDenyRequest {
  decisions: ApproveDenyDecision[]
}

export interface ApproveDenyResponse {
  approved: number
  denied: number
  errors: string[]
}

export interface UserIdentificationStats {
  user_id: number
  username: string
  full_name: string
  email: string
  face_count: number
  first_identification_date: string | null
  last_identification_date: string | null
}

export interface IdentificationReportResponse {
  items: UserIdentificationStats[]
  total_faces: number
  total_users: number
}

export interface ClearDatabaseResponse {
  deleted_records: number
  errors: string[]
}

export const pendingIdentificationsApi = {
  list: async (includeDenied: boolean = false): Promise<PendingIdentificationsListResponse> => {
    const res = await apiClient.get<PendingIdentificationsListResponse>(
      '/api/v1/pending-identifications',
      { params: { include_denied: includeDenied } }
    )
    return res.data
  },
  approveDeny: async (request: ApproveDenyRequest): Promise<ApproveDenyResponse> => {
    const res = await apiClient.post<ApproveDenyResponse>(
      '/api/v1/pending-identifications/approve-deny',
      request
    )
    return res.data
  },
  getReport: async (dateFrom?: string, dateTo?: string): Promise<IdentificationReportResponse> => {
    const params: Record<string, string> = {}
    if (dateFrom) params.date_from = dateFrom
    if (dateTo) params.date_to = dateTo
    const res = await apiClient.get<IdentificationReportResponse>(
      '/api/v1/pending-identifications/report',
      { params }
    )
    return res.data
  },
  clearDenied: async (): Promise<ClearDatabaseResponse> => {
    const res = await apiClient.post<ClearDatabaseResponse>(
      '/api/v1/pending-identifications/clear-denied'
    )
    return res.data
  },
}

export default pendingIdentificationsApi

