import apiClient from './client'

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

export interface VideoListItem {
  id: number
  filename: string
  path: string
  date_taken: string | null
  date_added: string
  identified_people: PersonInfo[]
  identified_people_count: number
}

export interface ListVideosResponse {
  items: VideoListItem[]
  page: number
  page_size: number
  total: number
}

export interface VideoPersonInfo {
  person_id: number
  first_name: string
  last_name: string
  middle_name?: string | null
  maiden_name?: string | null
  date_of_birth?: string | null
  identified_by: string | null
  identified_date: string
}

export interface VideoPeopleResponse {
  video_id: number
  people: VideoPersonInfo[]
}

export interface IdentifyVideoRequest {
  person_id?: number
  first_name?: string
  last_name?: string
  middle_name?: string
  maiden_name?: string
  date_of_birth?: string | null
}

export interface IdentifyVideoResponse {
  video_id: number
  person_id: number
  created_person: boolean
  message: string
}

export interface RemoveVideoPersonResponse {
  video_id: number
  person_id: number
  removed: boolean
  message: string
}

export const videosApi = {
  listVideos: async (params: {
    page?: number
    page_size?: number
    folder_path?: string
    date_from?: string
    date_to?: string
    has_people?: boolean
    person_name?: string
    sort_by?: string
    sort_dir?: string
  }): Promise<ListVideosResponse> => {
    const res = await apiClient.get<ListVideosResponse>('/api/v1/videos', { params })
    return res.data
  },

  getVideoPeople: async (videoId: number): Promise<VideoPeopleResponse> => {
    const res = await apiClient.get<VideoPeopleResponse>(`/api/v1/videos/${videoId}/people`)
    return res.data
  },

  identifyPerson: async (
    videoId: number,
    request: IdentifyVideoRequest
  ): Promise<IdentifyVideoResponse> => {
    const res = await apiClient.post<IdentifyVideoResponse>(
      `/api/v1/videos/${videoId}/identify`,
      request
    )
    return res.data
  },

  removePerson: async (
    videoId: number,
    personId: number
  ): Promise<RemoveVideoPersonResponse> => {
    const res = await apiClient.delete<RemoveVideoPersonResponse>(
      `/api/v1/videos/${videoId}/people/${personId}`
    )
    return res.data
  },

  getThumbnailUrl: (videoId: number): string => {
    const baseURL = import.meta.env.VITE_API_URL || 'http://127.0.0.1:8000'
    return `${baseURL}/api/v1/videos/${videoId}/thumbnail`
  },

  getVideoUrl: (videoId: number): string => {
    const baseURL = import.meta.env.VITE_API_URL || 'http://127.0.0.1:8000'
    return `${baseURL}/api/v1/videos/${videoId}/video`
  },
}

export default videosApi







