import { apiClient } from './api'

// TypeScript interfaces for custom task status types

export interface CustomTaskStatus {
  id: string
  name: string
  color: string
  order: number
  is_default: boolean
}

export interface SystemTaskStatus {
  id: string
  name: string
  color: string
  is_system: boolean
}

export interface AllTaskStatusesResponse {
  statuses: CustomTaskStatus[]
  system_statuses: SystemTaskStatus[]
  default_status_id: string
}

export interface CustomTaskStatusCreate {
  name: string
  color?: string
}

export interface CustomTaskStatusUpdate {
  name?: string
  color?: string
  is_default?: boolean
}

export interface CustomTaskStatusReorder {
  status_ids: string[]
}

export interface CustomTaskStatusDelete {
  reassign_to_status_id?: string
}

export interface TaskStatusInUseError {
  error: string
  status_id: string
  status_name: string
  task_count: number
  task_ids: number[]
}

export interface CustomTaskStatusResponse {
  message: string
  status?: CustomTaskStatus
  all_statuses?: AllTaskStatusesResponse
}

/**
 * Service for managing custom task statuses
 */
export const customTaskStatusService = {
  /**
   * Get all task statuses (system + custom) for a project
   * @param projectId - The project ID
   * @returns All task statuses including system and custom statuses
   */
  async getAllStatuses(projectId: number): Promise<AllTaskStatusesResponse> {
    const response = await apiClient.get(`/projects/${projectId}/task-statuses`)
    return response.data
  },

  /**
   * Create a new custom task status
   * @param projectId - The project ID
   * @param status - The status data to create
   * @returns The created status and all statuses
   */
  async createStatus(projectId: number, status: CustomTaskStatusCreate): Promise<CustomTaskStatusResponse> {
    const response = await apiClient.post(`/projects/${projectId}/task-statuses`, status)
    return response.data
  },

  /**
   * Update an existing custom task status
   * @param projectId - The project ID
   * @param statusId - The status ID to update
   * @param status - The status data to update
   * @returns The updated status and all statuses
   */
  async updateStatus(
    projectId: number,
    statusId: string,
    status: CustomTaskStatusUpdate
  ): Promise<CustomTaskStatusResponse> {
    const response = await apiClient.put(`/projects/${projectId}/task-statuses/${statusId}`, status)
    return response.data
  },

  /**
   * Delete a custom task status
   * @param projectId - The project ID
   * @param statusId - The status ID to delete
   * @param reassignToStatusId - Optional status ID to reassign tasks to if status is in use
   * @returns Success message and all remaining statuses
   */
  async deleteStatus(
    projectId: number,
    statusId: string,
    reassignToStatusId?: string
  ): Promise<CustomTaskStatusResponse> {
    const params = reassignToStatusId ? { reassign_to_status_id: reassignToStatusId } : {}
    const response = await apiClient.delete(`/projects/${projectId}/task-statuses/${statusId}`, {
      params
    })
    return response.data
  },

  /**
   * Reorder custom task statuses
   * @param projectId - The project ID
   * @param reorderData - The ordered list of status IDs
   * @returns Success message and all statuses in new order
   */
  async reorderStatuses(projectId: number, reorderData: CustomTaskStatusReorder): Promise<CustomTaskStatusResponse> {
    const response = await apiClient.patch(`/projects/${projectId}/task-statuses/reorder`, reorderData)
    return response.data
  }
}
