import { apiClient } from '@/shared/api/client'
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'

export interface AdminPickup {
  id: number
  pickup_no: string
  listing_id: number
  listing_no: string
  account_id: number
  account_title: string
  server_region: string
  login_platform: string
  owner_id: number
  owner_phone: string
  admin_id: number
  platform: string
  listing_price_cent: number
  owner_price_cent: number
  website_profit_cent: number
  profit_amount_cent: number
  seller_ratio: number
  buyer_ratio: number
  account_snapshot?: Record<string, unknown>
  settle_amount_cent: number
  status: string
  remark: string
  complete_remark: string
  created_at: string
  completed_at?: string
  cancelled_at?: string
}

export interface AvailableListing {
  id: number
  listing_no: string
  account_id: number
  account_title: string
  server_region: string
  login_platform: string
  owner_id: number
  owner_phone: string
  listing_price_cent: number
  owner_price_cent: number
  owner_pure_price_cent: number
  owner_extra_item_price_cent: number
  owner_total_price_cent: number
  website_profit_cent: number
  seller_ratio: number
  buyer_ratio: number
}

export interface AdminPickupCreateRequest {
  listing_id: number
  platform?: string
  profit_amount_cent?: number
  remark?: string
}

export interface AdminPickupCompleteRequest {
  settle_amount_cent: number
  profit_amount_cent?: number
  complete_remark?: string
}

export interface AdminPickupListQuery {
  page?: number
  page_size?: number
  keyword?: string
  status?: string
}

function cleanParams(query: object) {
  return Object.fromEntries(
    Object.entries(query).filter(([, value]) => value !== '' && value !== undefined)
  )
}

export async function fetchAdminPickups(query: AdminPickupListQuery = {}) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<AdminPickup>>>(
    '/admin/pickups',
    { params: cleanParams(query) }
  )
  const result = data.data
  return {
    items: Array.isArray(result?.items) ? result.items : [],
    total: Number(result?.total ?? 0),
    page: Number(result?.page ?? query.page ?? 1),
    page_size: Number(result?.page_size ?? query.page_size ?? 20),
  }
}

export async function fetchAdminPickup(id: string | number) {
  const { data } = await apiClient.get<ApiResponse<AdminPickup>>(`/admin/pickups/${id}`)
  return data.data
}

export async function fetchAvailableListings(keyword: string, page = 1, page_size = 20) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<AvailableListing>>>(
    '/admin/pickups/available-listings',
    { params: cleanParams({ keyword, page, page_size }) }
  )
  const result = data.data
  return {
    items: Array.isArray(result?.items) ? result.items : [],
    total: Number(result?.total ?? 0),
  }
}

export async function createAdminPickup(req: AdminPickupCreateRequest) {
  const { data } = await apiClient.post<ApiResponse<AdminPickup>>('/admin/pickups', req)
  return data.data
}

export async function completeAdminPickup(id: number, req: AdminPickupCompleteRequest) {
  const { data } = await apiClient.post<ApiResponse<AdminPickup>>(
    `/admin/pickups/${id}/complete`,
    req
  )
  return data.data
}

export async function cancelAdminPickup(id: number, reason: string) {
  const { data } = await apiClient.post<ApiResponse<{ cancelled: boolean }>>(
    `/admin/pickups/${id}/cancel`,
    { reason }
  )
  return data.data
}
