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

import {
  buildFeifeiPlatformOrderNo,
  resolveKuaishouFeifeiClaimUrl,
  resolveKuaishouFeifeiH5UrlWithUid,
  resolveKuaishouFeifeiPlatformAmount,
} from './index.js'
import type { TaskRow } from '../../../types/repository/rows.js'

test('resolveKuaishouFeifeiClaimUrl prefers feifei h5 url over local claim url', () => {
  assert.equal(
    resolveKuaishouFeifeiClaimUrl({
      claimUrl: 'https://ks.khhao.com/#/claim/local-token',
      h5: {
        entryUrl: 'https://feifei.example.com/h5/exchange',
        rechargeUrl: 'https://feifei.example.com/h5/bind?code=abc',
      },
    }),
    'https://feifei.example.com/h5/bind?code=abc',
  )
})

test('resolveKuaishouFeifeiClaimUrl does not expose local claim token fallback', () => {
  assert.equal(
    resolveKuaishouFeifeiClaimUrl({
      claimUrl: 'local-token',
      h5: {},
    }),
    '',
  )
})

test('resolveKuaishouFeifeiClaimUrl prefers direct h5 over historical short link url', () => {
  assert.equal(
    resolveKuaishouFeifeiClaimUrl({
      shortLink: {
        code: 'AbCd1234',
        url: 'https://ks.khhao.com/s/AbCd1234',
        targetUrl: 'https://feifei.example.com/h5/bind?code=very-long',
      },
      h5: {
        rechargeUrl: 'https://feifei.example.com/h5/bind?code=very-long',
      },
    }),
    'https://feifei.example.com/h5/bind?code=very-long',
  )
})

test('resolveKuaishouFeifeiH5UrlWithUid appends uid query', () => {
  const next = resolveKuaishouFeifeiH5UrlWithUid(
    {
      h5: {
        rechargeUrl: 'http://skin-exchange.yiquyou.icu/h5/bind?code=eyJpdi&product_name=demo',
      },
    },
    '166909256',
  )
  const url = new URL(next)
  assert.equal(url.searchParams.get('uid'), '166909256')
  assert.equal(url.searchParams.get('code'), 'eyJpdi')
})

test('buildFeifeiPlatformOrderNo prefers source order number', () => {
  assert.equal(
    buildFeifeiPlatformOrderNo(
      createTask({
        platform_order_id: 'KS202607080001',
        task_no: 'DT600a8bd23b8',
      }),
    ),
    'KS202607080001',
  )
})

test('buildFeifeiPlatformOrderNo falls back to task number', () => {
  assert.equal(
    buildFeifeiPlatformOrderNo(
      createTask({
        platform_order_id: '',
        task_no: 'DT600a8bd23b8',
      }),
    ),
    'DT600a8bd23b8',
  )
})

test('resolveKuaishouFeifeiPlatformAmount converts order fen amount to yuan', () => {
  assert.equal(
    resolveKuaishouFeifeiPlatformAmount({
      totalAmountFen: 3800,
      quantity: 1,
    }),
    38,
  )
})

test('resolveKuaishouFeifeiPlatformAmount splits amount by item quantity', () => {
  assert.equal(
    resolveKuaishouFeifeiPlatformAmount({
      totalAmountFen: 7600,
      quantity: 2,
    }),
    38,
  )
})

test('resolveKuaishouFeifeiPlatformAmount skips empty amount', () => {
  assert.equal(
    resolveKuaishouFeifeiPlatformAmount({
      totalAmountFen: 0,
      quantity: 1,
    }),
    0,
  )
})

function createTask(patch: Partial<TaskRow> = {}): TaskRow {
  return {
    id: 123,
    order_id: 456,
    order_item_id: 789,
    unit_index: 0,
    platform_order_id: '',
    profile_id: 0,
    task_no: '',
    executor_key: 'kuaishou_feifei',
    task_status: 'pending',
    delivery_status: 'pending',
    result_code: '',
    result_message: '',
    automation_mode: 'auto',
    requires_claim: true,
    user_action_status: 'not_required',
    attempt_count: 0,
    runtime_session_id: '',
    login_type: '',
    nickname: '',
    role_name: '',
    role_id: '',
    area: '',
    partition_name: '',
    claim_token: '',
    primary_claim_token: '',
    primary_claim_token_id: null,
    primary_claim_token_status: '',
    artifacts_json: '{}',
    context_json: '{}',
    screenshot_path: '',
    last_error: '',
    retry_count: 0,
    created_at: '2026-07-08T00:00:00.000Z',
    updated_at: '2026-07-08T00:00:00.000Z',
    claimed_at: null,
    role_confirmed_at: null,
    redeemed_at: null,
    ...patch,
  }
}
