import { describe, expect, it } from 'vitest'

import enUS from '@/i18n/locales/en-US'
import thTH from '@/i18n/locales/th-TH'
import zhCN from '@/i18n/locales/zh-CN'

describe('reservation i18n locales', () => {
  it('keeps Chinese, English, and Thai locale keys aligned', () => {
    const zhKeys = collectKeyPaths(zhCN)

    expect(collectKeyPaths(enUS)).toEqual(zhKeys)
    expect(collectKeyPaths(thTH)).toEqual(zhKeys)
  })

  it('includes Thai labels for the P0 reservation navigation', () => {
    expect(thTH.nav.orders).toBe('รายการออเดอร์')
    expect(thTH.nav.taskQueue).toBe('คิวงาน')
    expect(thTH.nav.debugEml).toBe('Debug EML')
    expect(thTH.taskList.viewConversation).toBe('ดูเธรดอีเมล')
  })

  it('covers Debug EML backend error codes in all locales', () => {
    const expectedErrorCodes = [
      'REQUEST_FIELD_REQUIRED',
      'EML_FILE_REQUIRED',
      'EML_FILE_TOO_LARGE',
      'INVALID_FILE_TYPE',
      'DEBUG_UPLOAD_KEY_INVALID',
      'EML_PARSE_FAILED',
      'OSS_UPLOAD_FAILED',
      'SUPERAGENT_OPEN_API_FAILED',
      'DEBUG_EML_RUN_FAILED',
    ] satisfies Array<keyof typeof zhCN.debugEml.errors>

    for (const locale of [zhCN, enUS, thTH]) {
      for (const errorCode of expectedErrorCodes) {
        expect(locale.debugEml.errors[errorCode]).toBeTruthy()
      }
    }
  })
})

function collectKeyPaths(value: unknown, prefix = ''): string[] {
  if (!isRecord(value)) {
    return [prefix]
  }
  return Object.keys(value)
    .sort()
    .flatMap((key) => collectKeyPaths(value[key], prefix ? `${prefix}.${key}` : key))
}

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null && !Array.isArray(value)
}
