import client from './client'
import type { ActiveEvent, MarathonEvent, EventCreate, SwapCandidate, SwapRequestItem, MySwapRequests, CommonEnemyLeaderboardEntry, EventAssignment, CompleteResult, GameChoiceChallenges } from '@/types'

export const eventsApi = {
  getActive: async (marathonId: number): Promise<ActiveEvent> => {
    const response = await client.get<ActiveEvent>(`/marathons/${marathonId}/event`)
    return response.data
  },

  list: async (marathonId: number): Promise<MarathonEvent[]> => {
    const response = await client.get<MarathonEvent[]>(`/marathons/${marathonId}/events`)
    return response.data
  },

  start: async (marathonId: number, data: EventCreate): Promise<MarathonEvent> => {
    const response = await client.post<MarathonEvent>(`/marathons/${marathonId}/events`, data)
    return response.data
  },

  stop: async (marathonId: number): Promise<void> => {
    await client.delete(`/marathons/${marathonId}/event`)
  },

  // Swap requests (two-sided confirmation)
  createSwapRequest: async (marathonId: number, targetParticipantId: number): Promise<SwapRequestItem> => {
    const response = await client.post<SwapRequestItem>(`/marathons/${marathonId}/swap-requests`, {
      target_participant_id: targetParticipantId,
    })
    return response.data
  },

  getSwapRequests: async (marathonId: number): Promise<MySwapRequests> => {
    const response = await client.get<MySwapRequests>(`/marathons/${marathonId}/swap-requests`)
    return response.data
  },

  acceptSwapRequest: async (marathonId: number, requestId: number): Promise<void> => {
    await client.post(`/marathons/${marathonId}/swap-requests/${requestId}/accept`)
  },

  declineSwapRequest: async (marathonId: number, requestId: number): Promise<void> => {
    await client.post(`/marathons/${marathonId}/swap-requests/${requestId}/decline`)
  },

  cancelSwapRequest: async (marathonId: number, requestId: number): Promise<void> => {
    await client.delete(`/marathons/${marathonId}/swap-requests/${requestId}`)
  },

  // Game Choice event
  getGameChoiceChallenges: async (marathonId: number, gameId: number): Promise<GameChoiceChallenges> => {
    const response = await client.get<GameChoiceChallenges>(`/marathons/${marathonId}/game-choice/challenges`, {
      params: { game_id: gameId },
    })
    return response.data
  },

  selectGameChoiceChallenge: async (marathonId: number, challengeId: number): Promise<{ message: string }> => {
    const response = await client.post<{ message: string }>(`/marathons/${marathonId}/game-choice/select`, {
      challenge_id: challengeId,
    })
    return response.data
  },

  getSwapCandidates: async (marathonId: number): Promise<SwapCandidate[]> => {
    const response = await client.get<SwapCandidate[]>(`/marathons/${marathonId}/swap-candidates`)
    return response.data
  },

  getCommonEnemyLeaderboard: async (marathonId: number): Promise<CommonEnemyLeaderboardEntry[]> => {
    const response = await client.get<CommonEnemyLeaderboardEntry[]>(`/marathons/${marathonId}/common-enemy-leaderboard`)
    return response.data
  },

  // Event Assignment (Common Enemy)
  getEventAssignment: async (marathonId: number): Promise<EventAssignment> => {
    const response = await client.get<EventAssignment>(`/marathons/${marathonId}/event-assignment`)
    return response.data
  },

  completeEventAssignment: async (
    assignmentId: number,
    data: { proof_url?: string; comment?: string; proof_file?: File }
  ): Promise<CompleteResult> => {
    const formData = new FormData()
    if (data.proof_url) formData.append('proof_url', data.proof_url)
    if (data.comment) formData.append('comment', data.comment)
    if (data.proof_file) formData.append('proof_file', data.proof_file)

    const response = await client.post<CompleteResult>(
      `/event-assignments/${assignmentId}/complete`,
      formData,
      { headers: { 'Content-Type': 'multipart/form-data' } }
    )
    return response.data
  },
}
