import { apiClient } from '@/shared/api/client'

import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
import type { DisputeStatus } from '@/types/status'

export interface Dispute {
  id: number
  order_id: number
  order_no: string
  title: string
  initiator_id: number
  target_user_id: number
  type: string
  status: DisputeStatus
  description: string
  evidence_urls?: string[]
  arbitration_result: string
  arbitration_remark: string
  handled_by?: number
  handled_at?: string
  created_at: string
  updated_at: string
}

export async function createDispute(orderId: number, payload: { type: string; description: string; evidence_urls?: string[] }) {
  const { data } = await apiClient.post<ApiResponse<Dispute>>(`/orders/${orderId}/dispute`, payload)
  return data.data
}

export async function fetchDisputes(page = 1, pageSize = 20) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<Dispute>>>('/disputes', {
    params: { page, page_size: pageSize },
  })
  return data.data
}

export async function fetchAdminDisputes(page = 1, pageSize = 20) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<Dispute>>>('/admin/disputes', {
    params: { page, page_size: pageSize },
  })
  return data.data
}

export async function arbitrateDispute(id: number, payload: { result: string; remark: string; amount?: number }) {
  const { data } = await apiClient.post<ApiResponse<Dispute>>(`/admin/disputes/${id}/arbitrate`, payload)
  return data.data
}
