import apiClient from './client'

export interface PhotoImportRequest {
  folder_path: string
  recursive?: boolean
}

export interface PhotoImportResponse {
  job_id: string
  message: string
  folder_path?: string
  estimated_photos?: number
}

export interface PhotoResponse {
  id: number
  path: string
  filename: string
  checksum?: string
  date_added: string
  date_taken?: string
  width?: number
  height?: number
  mime_type?: string
}

export interface UploadResponse {
  message: string
  added: number
  existing: number
  errors: string[]
}

export interface BulkDeletePhotosResponse {
  message: string
  deleted_count: number
  missing_photo_ids: number[]
}

export const photosApi = {
  importPhotos: async (
    request: PhotoImportRequest
  ): Promise<PhotoImportResponse> => {
    const { data } = await apiClient.post<PhotoImportResponse>(
      '/api/v1/photos/import',
      request
    )
    return data
  },

  uploadPhotos: async (files: File[]): Promise<UploadResponse> => {
    const formData = new FormData()
    files.forEach((file) => {
      formData.append('files', file)
    })

    // Don't set Content-Type header manually - let the browser set it with boundary
    const { data } = await apiClient.post<UploadResponse>(
      '/api/v1/photos/import/upload',
      formData
    )
    return data
  },

  getPhoto: async (photoId: number): Promise<PhotoResponse> => {
    const { data } = await apiClient.get<PhotoResponse>(
      `/api/v1/photos/${photoId}`
    )
    return data
  },

  streamJobProgress: (jobId: string): EventSource => {
    // EventSource needs absolute URL - use VITE_API_URL or fallback to direct backend URL
    const baseURL = import.meta.env.VITE_API_URL || 'http://127.0.0.1:8000'
    return new EventSource(`${baseURL}/api/v1/jobs/stream/${jobId}`)
  },

  searchPhotos: async (params: {
    search_type: 'name' | 'date' | 'tags' | 'no_faces' | 'no_tags' | 'processed' | 'unprocessed' | 'favorites'
    person_name?: string
    tag_names?: string
    match_all?: boolean
    date_from?: string
    date_to?: string
    folder_path?: string
    page?: number
    page_size?: number
  }): Promise<SearchPhotosResponse> => {
    const { data } = await apiClient.get<SearchPhotosResponse>('/api/v1/photos', {
      params,
    })
    return data
  },

  toggleFavorite: async (photoId: number): Promise<{ photo_id: number; is_favorite: boolean; message: string }> => {
    const { data } = await apiClient.post(
      `/api/v1/photos/${photoId}/toggle-favorite`
    )
    return data
  },

  checkFavorite: async (photoId: number): Promise<{ photo_id: number; is_favorite: boolean }> => {
    const { data } = await apiClient.get(
      `/api/v1/photos/${photoId}/is-favorite`
    )
    return data
  },

  bulkAddFavorites: async (photoIds: number[]): Promise<{ message: string; added_count: number; already_favorite_count: number; total_requested: number }> => {
    const { data } = await apiClient.post(
      '/api/v1/photos/bulk-add-favorites',
      { photo_ids: photoIds }
    )
    return data
  },

  bulkRemoveFavorites: async (photoIds: number[]): Promise<{ message: string; removed_count: number; not_favorite_count: number; total_requested: number }> => {
    const { data } = await apiClient.post(
      '/api/v1/photos/bulk-remove-favorites',
      { photo_ids: photoIds }
    )
    return data
  },

  bulkDeletePhotos: async (photoIds: number[]): Promise<BulkDeletePhotosResponse> => {
    const { data } = await apiClient.post<BulkDeletePhotosResponse>(
      '/api/v1/photos/bulk-delete',
      { photo_ids: photoIds }
    )
    return data
  },

  openFolder: async (photoId: number): Promise<{ message: string; folder: string }> => {
    const { data } = await apiClient.post<{ message: string; folder: string }>(
      `/api/v1/photos/${photoId}/open-folder`
    )
    return data
  },

  browseFolder: async (): Promise<{ path: string; success: boolean; message?: string }> => {
    const { data } = await apiClient.post<{ path: string; success: boolean; message?: string }>(
      '/api/v1/photos/browse-folder'
    )
    return data
  },
}

export interface PhotoSearchResult {
  id: number
  path: string
  filename: string
  date_taken?: string
  date_added: string
  processed: boolean
  person_name?: string
  tags: string[]
  has_faces: boolean
  face_count: number
  is_favorite?: boolean
}

export interface SearchPhotosResponse {
  items: PhotoSearchResult[]
  page: number
  page_size: number
  total: number
}

