import apiClient from './client'

export interface Person {
  id: number
  first_name: string
  last_name: string
  middle_name?: string | null
  maiden_name?: string | null
  date_of_birth?: string | null
}

export interface PeopleListResponse {
  items: Person[]
  total: number
}

export interface PersonWithFaces extends Person {
  face_count: number
  video_count: number
}

export interface PeopleWithFacesListResponse {
  items: PersonWithFaces[]
  total: number
}

export interface PersonCreateRequest {
  first_name: string
  last_name: string
  middle_name?: string
  maiden_name?: string
  date_of_birth?: string | null
}

export interface PersonUpdateRequest {
  first_name: string
  last_name: string
  middle_name?: string
  maiden_name?: string
  date_of_birth?: string | null
}

export const peopleApi = {
  list: async (lastName?: string): Promise<PeopleListResponse> => {
    const params = lastName ? { last_name: lastName } : {}
    const res = await apiClient.get<PeopleListResponse>('/api/v1/people', { params })
    return res.data
  },
  listWithFaces: async (lastName?: string): Promise<PeopleWithFacesListResponse> => {
    const params = lastName ? { last_name: lastName } : {}
    const res = await apiClient.get<PeopleWithFacesListResponse>('/api/v1/people/with-faces', { params })
    return res.data
  },
  create: async (payload: PersonCreateRequest): Promise<Person> => {
    const res = await apiClient.post<Person>('/api/v1/people', payload)
    return res.data
  },
  update: async (personId: number, payload: PersonUpdateRequest): Promise<Person> => {
    const res = await apiClient.put<Person>(`/api/v1/people/${personId}`, payload)
    return res.data
  },
  getFaces: async (personId: number): Promise<PersonFacesResponse> => {
    const res = await apiClient.get<PersonFacesResponse>(`/api/v1/people/${personId}/faces`)
    return res.data
  },
  getVideos: async (personId: number): Promise<PersonVideosResponse> => {
    const res = await apiClient.get<PersonVideosResponse>(`/api/v1/people/${personId}/videos`)
    return res.data
  },
  acceptMatches: async (personId: number, faceIds: number[]): Promise<IdentifyFaceResponse> => {
    const res = await apiClient.post<IdentifyFaceResponse>(`/api/v1/people/${personId}/accept-matches`, { face_ids: faceIds })
    return res.data
  },
  delete: async (personId: number): Promise<void> => {
    await apiClient.delete(`/api/v1/people/${personId}`)
  },
}

export interface IdentifyFaceResponse {
  identified_face_ids: number[]
  person_id: number
  created_person: boolean
}

export interface PersonFaceItem {
  id: number
  photo_id: number
  photo_path: string
  photo_filename: string
  location: string
  face_confidence: number
  quality_score: number
  detector_backend: string
  model_name: string
}

export interface PersonFacesResponse {
  person_id: number
  items: PersonFaceItem[]
  total: number
}

export interface PersonVideoItem {
  id: number
  filename: string
  path: string
  date_taken: string | null
  date_added: string
  linkage_id: number
}

export interface PersonVideosResponse {
  person_id: number
  items: PersonVideoItem[]
  total: number
}

export default peopleApi



