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

import type { AppConfig } from "@qjclaw/shared-types"
import {
  getHasPendingSettingsChange,
  getResetBasicSettingsDrafts,
  getResetCopywritingSettingsDrafts,
  getResetDigitalHumanSettingsDrafts,
  getResetDouyinRuntimeSettingsDrafts,
  getResetImageSettingsDrafts,
  getResetVideoSettingsDrafts,
  getResetXhsFeishuSettingsDrafts
} from "../src/features/settings/settingsDrafts.ts"

const config = {
  setupMode: "employee-key",
  provider: "openai",
  baseUrl: "https://example.test/v1",
  apiKeyConfigured: true,
  gatewayTokenConfigured: false,
  authTokenConfigured: false,
  defaultModel: "qwen-max",
  workspacePath: "/workspace/current",
  gatewayUrl: "ws://127.0.0.1:8765",
  cloudApiBaseUrl: "https://cloud.example.test",
  runtimeCloudApiBaseUrl: "https://runtime.example.test",
  runtimeMode: "bundled-runtime",
  expertModelConfig: {
    image: { baseUrl: "https://image.example.test", modelId: "image-model", apiKeyConfigured: true },
    video: { baseUrl: "https://video.example.test", modelId: "video-model", apiKeyConfigured: true },
    copywriting: { baseUrl: "https://copy.example.test", modelId: "copy-model", apiKeyConfigured: true },
    digitalHuman: {
      volcRegion: "cn-north-1",
      volcService: "cv",
      volcHost: "visual.volcengineapi.com",
      volcScheme: "https",
      ttsVoice: "zh-CN-YunxiNeural",
      qiniuBucket: "bucket",
      qiniuDomain: "https://cdn.example.test",
      qiniuKeyPrefix: "omnihuman",
      volcAccessKeyConfigured: true,
      volcSecretKeyConfigured: true,
      qiniuAccessKeyConfigured: true,
      qiniuSecretKeyConfigured: true
    }
  },
  douyinRuntimeConfig: {
    videoAnalyzer: { baseUrl: "https://video-analyzer.example.test", modelId: "va-model", apiKeyConfigured: true },
    replicationBrief: { baseUrl: "https://brief.example.test", modelId: "brief-model", apiKeyConfigured: true },
    vectcut: { baseUrl: "https://vectcut.example.test", fileBaseUrl: "https://files.example.test", apiKeyConfigured: true }
  },
  xhsFeishuConfig: {
    appIdConfigured: true,
    appSecretConfigured: true,
    appTokenConfigured: true,
    tableIdConfigured: true
  }
} satisfies AppConfig

test("detects any pending settings change", () => {
  assert.equal(getHasPendingSettingsChange({
    hasPendingBasicConfig: false,
    hasPendingXhsFeishuConfig: false,
    hasPendingCopywritingConfig: false,
    hasPendingImageConfig: false,
    hasPendingVideoConfig: false,
    hasPendingDigitalHumanConfig: false,
    hasPendingDouyinRuntimeConfig: false
  }), false)

  assert.equal(getHasPendingSettingsChange({
    hasPendingBasicConfig: false,
    hasPendingXhsFeishuConfig: false,
    hasPendingCopywritingConfig: false,
    hasPendingImageConfig: false,
    hasPendingVideoConfig: false,
    hasPendingDigitalHumanConfig: false,
    hasPendingDouyinRuntimeConfig: true
  }), true)
})

test("basic reset only affects lobster key and workspace path", () => {
  assert.deepEqual(getResetBasicSettingsDrafts(config), {
    lobsterKey: "",
    workspacePath: "/workspace/current"
  })
})

test("single model resets restore saved endpoints and clear the current module key", () => {
  assert.deepEqual(getResetCopywritingSettingsDrafts(config), {
    copywritingModelBaseUrl: "https://copy.example.test",
    copywritingModelModelId: "copy-model",
    copywritingModelApiKey: "",
  })
  assert.deepEqual(getResetImageSettingsDrafts(config), {
    imageModelBaseUrl: "https://image.example.test",
    imageModelModelId: "image-model",
    imageModelApiKey: "",
  })
  assert.deepEqual(getResetVideoSettingsDrafts(config), {
    videoModelBaseUrl: "https://video.example.test",
    videoModelModelId: "video-model",
    videoModelApiKey: "",
  })
})

test("xhs feishu reset only clears its own drafts", () => {
  assert.deepEqual(getResetXhsFeishuSettingsDrafts(), {
    xhsFeishuAppId: "",
    xhsFeishuAppSecret: "",
    xhsFeishuAppToken: "",
    xhsFeishuTableId: ""
  })
})

test("digital human reset only clears its own secrets", () => {
  assert.deepEqual(getResetDigitalHumanSettingsDrafts(), {
    digitalHumanVolcAccessKey: "",
    digitalHumanVolcSecretKey: "",
    digitalHumanQiniuAccessKey: "",
    digitalHumanQiniuSecretKey: ""
  })
})

test("douyin runtime reset restores saved base urls and model ids while clearing runtime secrets", () => {
  assert.deepEqual(getResetDouyinRuntimeSettingsDrafts(config), {
    videoAnalyzerBaseUrl: "https://video-analyzer.example.test",
    videoAnalyzerModelId: "va-model",
    videoAnalyzerApiKey: "",
    replicationBriefBaseUrl: "https://brief.example.test",
    replicationBriefModelId: "brief-model",
    replicationBriefApiKey: "",
    vectcutBaseUrl: "https://vectcut.example.test",
    vectcutFileBaseUrl: "https://files.example.test",
    vectcutApiKey: ""
  })
})
