import { apiGet, apiPost } from '@/lib/http'
import type {
  CollectLookupResponse,
  UploadedFile,
  WorkOrder,
  WorkOrderShare,
  WorkerFinanceRequest,
  WorkerHallOrdersResponse,
  WorkerListResponse,
  WorkerLoginResponse,
  WorkerProfileResponse,
  WorkerUser,
  WorkerWalletLedger,
} from '@/types/worker-platform'

export function sendWorkerSmsCode(payload: { phone: string }) {
  return apiPost<{ sent: boolean; expiresInSeconds: number; provider: string }>(
    '/api/v1/worker/auth/sms-code',
    payload,
  )
}

export function registerWorker(payload: {
  phone: string
  code: string
  password: string
  displayName: string
  inviteCode?: string
}) {
  return apiPost<{ worker: WorkerUser; reviewRequired: boolean }>(
    '/api/v1/worker/auth/register',
    payload,
  )
}

export function loginWorker(payload: { username: string; password: string }) {
  return apiPost<WorkerLoginResponse>('/api/v1/worker/auth/login', payload)
}

export function fetchWorkerSession() {
  return apiGet<{
    authenticated: boolean
    expiresAt: string
    worker: WorkerUser
  }>('/api/v1/worker/auth/session')
}

export function logoutWorker() {
  return apiPost<{ success: boolean }>('/api/v1/worker/auth/logout')
}

export function fetchWorkerProfile() {
  return apiGet<WorkerProfileResponse>('/api/v1/worker/profile')
}

export function fetchWorkerWalletLedgers(params?: Record<string, unknown>) {
  return apiGet<WorkerListResponse<WorkerWalletLedger>>(
    '/api/v1/worker/profile/wallet-ledgers',
    params,
  )
}

export function fetchWorkerFinanceRequests(params?: Record<string, unknown>) {
  return apiGet<WorkerListResponse<WorkerFinanceRequest>>(
    '/api/v1/worker/profile/finance-requests',
    params,
  )
}

export function createWorkerRechargeRequest(payload: {
  amount?: number
  note?: string
  paidAt?: string
  proofFiles?: UploadedFile[]
}) {
  return apiPost<{ request: WorkerFinanceRequest }>(
    '/api/v1/worker/profile/recharge-requests',
    payload,
  )
}

export function createWorkerWithdrawRequest(payload: {
  amount?: number
  accountChannel?: string
  accountName?: string
  accountNo?: string
  note?: string
}) {
  return apiPost<{ request: WorkerFinanceRequest }>(
    '/api/v1/worker/profile/withdraw-requests',
    payload,
  )
}

export function changeWorkerPassword(payload: {
  currentPassword: string
  newPassword: string
}) {
  return apiPost<{ worker: WorkerUser; reloginRequired: boolean }>(
    '/api/v1/worker/profile/change-password',
    payload,
  )
}

export function fetchWorkerHallOrders(params?: Record<string, unknown>) {
  return apiGet<WorkerHallOrdersResponse>(
    '/api/v1/worker/hall/orders',
    params,
  )
}

export function grabWorkerOrder(workOrderId: number) {
  return apiPost<{ order: WorkOrder }>(
    `/api/v1/worker/hall/orders/${workOrderId}/grab`,
  )
}

export function joinWorkerSharingOrder(workOrderId: number, payload: { quantity: number }) {
  return apiPost<{ share: WorkOrderShare }>(
    `/api/v1/worker/hall/orders/${workOrderId}/join-sharing`,
    payload,
  )
}

export function fetchWorkerMyOrders(params?: Record<string, unknown>) {
  return apiGet<WorkerListResponse<WorkOrder>>('/api/v1/worker/orders', params)
}

export function submitWorkerAcceptance(
  workOrderId: number,
  payload: { note?: string; imageUrls?: string[]; files?: UploadedFile[] },
) {
  return apiPost<{ order: WorkOrder }>(
    `/api/v1/worker/orders/${workOrderId}/submit-acceptance`,
    payload,
  )
}

export function cancelWorkerOrder(workOrderId: number) {
  return apiPost<{ order: WorkOrder }>(
    `/api/v1/worker/orders/${workOrderId}/cancel`,
  )
}

export function lookupCollectOrder(orderNo: string) {
  return apiPost<CollectLookupResponse>('/api/v1/collect/lookup', { orderNo })
}

export function submitCollectOrder(payload: {
  orderNo: string
  workOrderId?: number
  fields: Record<string, string>
  screenshots?: UploadedFile[]
}) {
  return apiPost<{ complete: boolean; order: Record<string, unknown> }>(
    '/api/v1/collect/submit',
    payload,
  )
}
