import assert from 'node:assert/strict'
import test from 'node:test'

import {
  getFulfillmentExecutor,
  prepareFulfillmentBinding,
  redeemFulfillmentTask,
  dispatchFulfillmentTask,
  confirmFulfillmentRole,
  rebindFulfillmentRole,
  refreshFulfillmentRole,
  returnFulfillmentNumber,
} from './registry.js'
import { FULFILLMENT_EXECUTOR_KEYS } from './types.js'
import type { TaskRow } from '../../../types/repository/rows.js'

function makeTask(executorKey: string): TaskRow {
  return {
    id: 1,
    executor_key: executorKey,
  } as TaskRow
}

test('getFulfillmentExecutor 映射 lewan / industry / feifei / affiliate_dash / manual', () => {
  assert.equal(
    getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_CLOUD)?.key,
    FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_CLOUD,
  )
  assert.equal(
    getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_INDUSTRY)?.key,
    FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_CLOUD,
  )
  assert.equal(
    getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_FEIFEI)?.key,
    FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_FEIFEI,
  )
  assert.equal(
    getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.AFFILIATE_DASH)?.key,
    FULFILLMENT_EXECUTOR_KEYS.AFFILIATE_DASH,
  )
  assert.equal(
    getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.MANUAL_DISPATCH)?.key,
    FULFILLMENT_EXECUTOR_KEYS.MANUAL_DISPATCH,
  )
  assert.equal(getFulfillmentExecutor('unknown'), null)
})

test('affiliate_dash executor 暴露 preparePaidTask 与 resolveDeliveryLink', () => {
  const executor = getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.AFFILIATE_DASH)
  assert.ok(executor)
  assert.equal(typeof executor.preparePaidTask, 'function')
  assert.equal(typeof executor.resolveDeliveryLink, 'function')
  // affiliate_dash 不需要 lewan 专属动作
  assert.equal(executor.prepareBinding, undefined)
  assert.equal(executor.redeemTask, undefined)
})

test('lewan executor 暴露完整履约动作', () => {
  const executor = getFulfillmentExecutor(FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_CLOUD)
  assert.ok(executor)
  assert.equal(typeof executor.prepareBinding, 'function')
  assert.equal(typeof executor.rebindRole, 'function')
  assert.equal(typeof executor.refreshRole, 'function')
  assert.equal(typeof executor.confirmRole, 'function')
  assert.equal(typeof executor.redeemTask, 'function')
  assert.equal(typeof executor.dispatchTask, 'function')
  assert.equal(typeof executor.returnNumber, 'function')
})

test('feifei / manual 不支持 lewan 专属动作时 registry 返回 null', async () => {
  const feifei = makeTask(FULFILLMENT_EXECUTOR_KEYS.KUAISHOU_FEIFEI)
  const manual = makeTask(FULFILLMENT_EXECUTOR_KEYS.MANUAL_DISPATCH)

  assert.equal(await prepareFulfillmentBinding(feifei), null)
  assert.equal(await rebindFulfillmentRole(feifei), null)
  assert.equal(await refreshFulfillmentRole(feifei), null)
  assert.equal(await confirmFulfillmentRole(feifei), null)
  assert.equal(await redeemFulfillmentTask(feifei), null)
  assert.equal(await dispatchFulfillmentTask(feifei), null)
  assert.equal(await returnFulfillmentNumber(feifei), null)

  assert.equal(await redeemFulfillmentTask(manual), null)
  assert.equal(await dispatchFulfillmentTask(manual), null)
})
