import { apiDelete, apiGet, apiPost } from '@/lib/http'
import type {
  AdminCloudtentaclesLoginTestResult,
  AdminCloudtentaclesDeliveryRecordListResult,
  AdminCloudtentaclesOverrideRuleConfig,
  AdminCloudtentaclesSkuListResult,
  AdminCloudtentaclesSendSmsResult,
  AdminCloudtentaclesSourceConfigResponse,
  AdminCloudtentaclesSourceItem,
  AdminCloudtentaclesSourcesConfig,
  AdminCloudtentaclesValidateSessionResult,
  AdminCloudtentaclesVnListResult,
} from '@/types/admin'

export function fetchAdminCloudtentaclesSourceConfig() {
  return apiGet<AdminCloudtentaclesSourceConfigResponse>(
    '/api/v1/admin/platform-config/cloudtentacles-source',
  )
}

export function saveAdminCloudtentaclesSourceConfig(
  payload:
    | AdminCloudtentaclesSourcesConfig
    | ({ sourceKey: string } & Partial<AdminCloudtentaclesSourceItem>),
) {
  return apiPost<AdminCloudtentaclesSourceConfigResponse>(
    '/api/v1/admin/platform-config/cloudtentacles-source',
    payload,
  )
}

export function deleteAdminCloudtentaclesSource(sourceKey: string) {
  return apiDelete<{ success: boolean }>(
    `/api/v1/admin/platform-config/cloudtentacles-source/${sourceKey}`,
  )
}

export function fetchAdminCloudtentaclesOverrideRules() {
  return apiGet<AdminCloudtentaclesOverrideRuleConfig>(
    '/api/v1/admin/platform-config/cloudtentacles/override-rules',
  )
}

export function saveAdminCloudtentaclesOverrideRules(payload: {
  enabled: boolean
  rules: Array<{
    id?: string
    enabled?: boolean
    productName?: string
    sourceKey?: string
    deliveryItems?: Array<{
      cloudSkuId?: number
      cloudSkuName?: string
      quantity?: number
    }>
    notes?: string
  }>
}) {
  return apiPost<AdminCloudtentaclesOverrideRuleConfig>(
    '/api/v1/admin/platform-config/cloudtentacles/override-rules',
    payload,
  )
}

export function sendAdminCloudtentaclesSmsCode(payload: {
  baseUrl?: string
  username?: string
  phone?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<AdminCloudtentaclesSendSmsResult>(
    '/api/v1/admin/platform-config/cloudtentacles/send-sms-code',
    payload,
  )
}

export function testAdminCloudtentaclesLogin(payload: {
  baseUrl?: string
  username?: string
  password?: string
  phone?: string
  code?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<AdminCloudtentaclesLoginTestResult>(
    '/api/v1/admin/platform-config/cloudtentacles/test-login',
    payload,
  )
}

export function validateAdminCloudtentaclesSession(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<AdminCloudtentaclesValidateSessionResult>(
    '/api/v1/admin/platform-config/cloudtentacles/validate-session',
    payload,
  )
}

export function fetchAdminCloudtentaclesAsset(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/asset',
    payload,
  )
}

export function fetchAdminCloudtentaclesCategories(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/categories',
    payload,
  )
}

export function fetchAdminCloudtentaclesSkuList(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<AdminCloudtentaclesSkuListResult>(
    '/api/v1/admin/platform-config/cloudtentacles/sku/list',
    payload,
  )
}

export function buyAdminCloudtentaclesSku(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  id?: number
  count?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/sku/buy',
    payload,
  )
}

export function useAdminCloudtentaclesSku(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  id?: number
  virtualNumberId?: number
  phone?: string
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/sku/use',
    payload,
  )
}

export function fetchAdminCloudtentaclesKnapsack(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/knapsack',
    payload,
  )
}

export function fetchAdminCloudtentaclesDeliveryRecords(payload: {
  sourceKey?: string
  page?: number
  size?: number
  startDate?: string
  endDate?: string
  platformOrderId?: string
}) {
  return apiPost<AdminCloudtentaclesDeliveryRecordListResult>(
    '/api/v1/admin/cloudtentacles-records',
    payload,
  )
}

export function fetchAdminCloudtentaclesRecordSources() {
  return apiGet<{
    sources: Array<{
      key: string
      label: string
      enabled: boolean
      hasToken: boolean
      loggedInAt: string
    }>
  }>('/api/v1/admin/cloudtentacles-records/sources')
}

export function fetchAdminCloudtentaclesVnList(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  sourceKey?: string
}) {
  return apiPost<AdminCloudtentaclesVnListResult>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/list',
    payload,
  )
}

export function appointAdminCloudtentaclesVn(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/appoint',
    payload,
  )
}

export function generateAdminCloudtentaclesVnLoginCode(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  id?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/generate-login-code',
    payload,
  )
}

export function fetchAdminCloudtentaclesVnCode(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  phone?: string
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/fetch-code',
    payload,
  )
}

export function verifyAdminCloudtentaclesVnCode(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  id?: number
  code?: string
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/verify-code',
    payload,
  )
}

export function fetchAdminCloudtentaclesBindUrl(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  id?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/bind-url',
    payload,
  )
}

export function backAdminCloudtentaclesVn(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  key?: string
  id?: number
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/vn/back',
    payload,
  )
}

export function runAdminCloudtentaclesFullFlow(payload: {
  baseUrl?: string
  token?: string
  deviceId?: string
  deviceType?: number
  skuId?: number
  skuCount?: number
  vnKey?: string
  sourceKey?: string
}) {
  return apiPost<Record<string, unknown>>(
    '/api/v1/admin/platform-config/cloudtentacles/debug/full-flow',
    payload,
  )
}
