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

import { generateKuaishouIndustryVoucherCode } from '../../../repositories/kuaishou-industry-voucher-repo.js'
import { buildKuaishouIndustryVoucherContext } from './voucher-binding-service.js'
import {
  buildKuaishouIndustryEticketFromVoucher,
  normalizeKuaishouIndustryConsumeType,
  normalizeKuaishouIndustryDestroyReason,
  resolveKuaishouIndustryEticketType,
  resolveKuaishouIndustryVoucherValidity,
} from './voucher-service.js'

test('generateKuaishouIndustryVoucherCode creates non-numeric stable voucher code', () => {
  let requestedSize = 0
  const code = generateKuaishouIndustryVoucherCode((size) => {
    requestedSize = size
    return Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
  })

  assert.equal(requestedSize, 10)
  assert.equal(code.length, 19)
  assert.match(code, /^KSV[0-9ABCDEFGHJKMNPQRSTVWXYZ]{16}$/)
  assert.doesNotMatch(code, /^\d+$/)
  assert.doesNotMatch(code, /[ILOU]/)
})

test('generateKuaishouIndustryVoucherCode changes with random bytes', () => {
  const first = generateKuaishouIndustryVoucherCode(() => Buffer.alloc(10, 0))
  const second = generateKuaishouIndustryVoucherCode(() => Buffer.alloc(10, 1))

  assert.notEqual(first, second)
})

test('resolveKuaishouIndustryVoucherValidity uses certExpDays when explicit times are zero', () => {
  const nowMs = 1783394218325
  const validity = resolveKuaishouIndustryVoucherValidity(
    {
      certActualStartTime: 0,
      certActualEndTime: 0,
      certStartTime: 0,
      certEndTime: 0,
      certExpDays: 3,
    },
    nowMs,
  )

  assert.equal(validity.validStartTime, nowMs)
  assert.equal(validity.validEndTime, nowMs + 3 * 86_400_000)
})

test('resolveKuaishouIndustryVoucherValidity defaults to thirty days', () => {
  const nowMs = 1783394218325
  const validity = resolveKuaishouIndustryVoucherValidity({}, nowMs)

  assert.equal(validity.validStartTime, nowMs)
  assert.equal(validity.validEndTime, nowMs + 30 * 86_400_000)
  assert.equal(validity.certExpDays, 30)
})

test('resolveKuaishouIndustryVoucherValidity prefers actual certificate time range', () => {
  const validity = resolveKuaishouIndustryVoucherValidity(
    {
      certActualStartTime: 1000,
      certActualEndTime: 5000,
      certStartTime: 2000,
      certEndTime: 6000,
      certExpDays: 3,
    },
    9000,
  )

  assert.equal(validity.validStartTime, 1000)
  assert.equal(validity.validEndTime, 5000)
})

test('normalizeKuaishouIndustryConsumeType defaults to consume', () => {
  assert.equal(normalizeKuaishouIndustryConsumeType(undefined), 'consume')
  assert.equal(normalizeKuaishouIndustryConsumeType(''), 'consume')
  assert.equal(normalizeKuaishouIndustryConsumeType(' pickup '), 'pickup')
})

test('buildKuaishouIndustryEticketFromVoucher mirrors voucher code into id and code', () => {
  const eticket = buildKuaishouIndustryEticketFromVoucher(
    {
      voucher_code: 'KSV15K821FDHPMTX1ST',
      status: 'UNUSED',
      valid_start_time: 1783427213868,
      valid_end_time: 1786019213868,
    } as any,
    'GAME_OPEN_TICKET_CONSUME',
  )

  assert.equal(eticket.id, 'KSV15K821FDHPMTX1ST')
  assert.equal(eticket.code, 'KSV15K821FDHPMTX1ST')
  assert.equal(eticket.eticketType, 'GAME_OPEN_TICKET_CONSUME')
})

test('resolveKuaishouIndustryEticketType prefers explicit then column then raw payload', () => {
  assert.equal(
    resolveKuaishouIndustryEticketType(
      {
        eticket_type: 'GAME_OPEN_TICKET_CONSUME',
        raw_payload_json: { body: { eticketType: 'DINING_OPEN_TICKET' } },
      } as any,
      'EXPLICIT_TYPE',
    ),
    'EXPLICIT_TYPE',
  )

  assert.equal(
    resolveKuaishouIndustryEticketType({
      eticket_type: 'GAME_OPEN_TICKET_CONSUME',
      raw_payload_json: { body: { eticketType: 'DINING_OPEN_TICKET' } },
    } as any),
    'GAME_OPEN_TICKET_CONSUME',
  )

  assert.equal(
    resolveKuaishouIndustryEticketType({
      eticket_type: '',
      raw_payload_json: { body: { eticketType: 'GAME_OPEN_TICKET_CONSUME' } },
    } as any),
    'GAME_OPEN_TICKET_CONSUME',
  )
})

test('normalizeKuaishouIndustryDestroyReason defaults to SYS_ADMIN_DESTROY', () => {
  assert.equal(normalizeKuaishouIndustryDestroyReason(''), 'SYS_ADMIN_DESTROY')
  assert.equal(normalizeKuaishouIndustryDestroyReason('supply_destroy'), 'SUPPLY_DESTROY')
  assert.equal(normalizeKuaishouIndustryDestroyReason('unknown'), 'SYS_ADMIN_DESTROY')
})

test('buildKuaishouIndustryVoucherContext maps consumed voucher state', () => {
  const context = buildKuaishouIndustryVoucherContext(
    {
      oid: '2618800481389561',
      voucher_code: 'KSV15K821FDHPMTX1ST',
      token: 'ticket-token',
      unit_index: 1,
      status: 'CONSUMED',
      valid_start_time: 1783427213868,
      valid_end_time: 1786019213868,
      consumed_at: '2026-07-07T12:36:54.626Z',
    } as any,
    {
      verifiedAt: '2026-07-07T12:26:58.421Z',
    },
    '2026-07-07T12:36:54.626Z',
  )

  assert.equal(context.status, 'CONSUMED')
  assert.equal(context.consumedAt, '2026-07-07T12:36:54.626Z')
  assert.equal(context.voucherCode, 'KSV15K821FDHPMTX1ST')
  assert.equal(context.verifiedAt, '2026-07-07T12:26:58.421Z')
})
