import { apiClient } from './api'

export interface Episode {
  id: number
  project_id: number
  name: string
  description?: string
  episode_number: number
  status: 'planning' | 'in_progress' | 'on_hold' | 'completed' | 'cancelled'
  created_at: string
  updated_at: string
  shot_count?: number
  progress?: number // Progress percentage (0-100)
  completed_shots?: number // Number of completed shots
}

export interface EpisodeCreate {
  name: string
  description?: string
  episode_number: number
  status?: 'planning' | 'in_progress' | 'on_hold' | 'completed' | 'cancelled'
}

export interface EpisodeUpdate {
  name?: string
  description?: string
  episode_number?: number
  status?: 'planning' | 'in_progress' | 'on_hold' | 'completed' | 'cancelled'
}

export const episodeService = {
  async getEpisodes(projectId?: number): Promise<Episode[]> {
    const params = projectId ? { project_id: projectId } : {}
    const response = await apiClient.get('/episodes/', { params })
    return response.data
  },

  async getProjectEpisodes(projectId: number): Promise<Episode[]> {
    const response = await apiClient.get(`/episodes/projects/${projectId}/episodes`)
    return response.data
  },

  async getEpisode(episodeId: number): Promise<Episode> {
    const response = await apiClient.get(`/episodes/${episodeId}`)
    return response.data
  },

  async createEpisode(projectId: number, episode: EpisodeCreate): Promise<Episode> {
    const response = await apiClient.post(`/episodes/projects/${projectId}/episodes`, episode)
    return response.data
  },

  async updateEpisode(episodeId: number, episode: EpisodeUpdate): Promise<Episode> {
    const response = await apiClient.put(`/episodes/${episodeId}`, episode)
    return response.data
  },

  async deleteEpisode(episodeId: number): Promise<void> {
    await apiClient.delete(`/episodes/${episodeId}`)
  }
}