import type { AppConfig } from "@qjclaw/shared-types"

export interface PendingSettingsFlags {
  hasPendingBasicConfig: boolean
  hasPendingXhsFeishuConfig: boolean
  hasPendingCopywritingConfig: boolean
  hasPendingImageConfig: boolean
  hasPendingVideoConfig: boolean
  hasPendingDigitalHumanConfig: boolean
  hasPendingDouyinRuntimeConfig: boolean
  hasPendingFeishuMobileConfig: boolean
}

export interface BasicResetSettingsDrafts {
  lobsterKey: string
  workspacePath: string
}

export interface DigitalHumanResetSettingsDrafts {
  digitalHumanVolcAccessKey: string
  digitalHumanVolcSecretKey: string
  digitalHumanQiniuAccessKey: string
  digitalHumanQiniuSecretKey: string
}

export interface CopywritingResetSettingsDrafts {
  copywritingModelBaseUrl: string
  copywritingModelModelId: string
  copywritingModelApiKey: string
}

export interface ImageResetSettingsDrafts {
  imageModelBaseUrl: string
  imageModelModelId: string
  imageModelApiKey: string
}

export interface VideoResetSettingsDrafts {
  videoModelBaseUrl: string
  videoModelModelId: string
  videoModelApiKey: string
}

export interface DouyinRuntimeResetSettingsDrafts {
  videoAnalyzerBaseUrl: string
  videoAnalyzerModelId: string
  videoAnalyzerApiKey: string
  replicationBriefBaseUrl: string
  replicationBriefModelId: string
  replicationBriefApiKey: string
  vectcutBaseUrl: string
  vectcutFileBaseUrl: string
  vectcutApiKey: string
}

export interface XhsFeishuResetSettingsDrafts {
  xhsFeishuAppId: string
  xhsFeishuAppSecret: string
  xhsFeishuAppToken: string
  xhsFeishuTableId: string
}

export interface FeishuMobileResetSettingsDrafts {
  feishuMobileAppId: string
  feishuMobileAppSecret: string
}

export function getHasPendingSettingsChange(flags: PendingSettingsFlags) {
  return flags.hasPendingBasicConfig
    || flags.hasPendingXhsFeishuConfig
    || flags.hasPendingCopywritingConfig
    || flags.hasPendingImageConfig
    || flags.hasPendingVideoConfig
    || flags.hasPendingDigitalHumanConfig
    || flags.hasPendingDouyinRuntimeConfig
    || flags.hasPendingFeishuMobileConfig
}

export function getResetBasicSettingsDrafts(config: AppConfig): BasicResetSettingsDrafts {
  return {
    lobsterKey: "",
    workspacePath: config.workspacePath
  }
}

export function getResetCopywritingSettingsDrafts(config: AppConfig): CopywritingResetSettingsDrafts {
  return {
    copywritingModelBaseUrl: config.expertModelConfig.copywriting.baseUrl,
    copywritingModelModelId: config.expertModelConfig.copywriting.modelId ?? "",
    copywritingModelApiKey: ""
  }
}

export function getResetImageSettingsDrafts(config: AppConfig): ImageResetSettingsDrafts {
  return {
    imageModelBaseUrl: config.expertModelConfig.image.baseUrl,
    imageModelModelId: config.expertModelConfig.image.modelId ?? "",
    imageModelApiKey: ""
  }
}

export function getResetVideoSettingsDrafts(config: AppConfig): VideoResetSettingsDrafts {
  return {
    videoModelBaseUrl: config.expertModelConfig.video.baseUrl,
    videoModelModelId: config.expertModelConfig.video.modelId ?? "",
    videoModelApiKey: ""
  }
}

export function getResetDigitalHumanSettingsDrafts(): DigitalHumanResetSettingsDrafts {
  return {
    digitalHumanVolcAccessKey: "",
    digitalHumanVolcSecretKey: "",
    digitalHumanQiniuAccessKey: "",
    digitalHumanQiniuSecretKey: ""
  }
}

export function getResetDouyinRuntimeSettingsDrafts(config: AppConfig): DouyinRuntimeResetSettingsDrafts {
  return {
    videoAnalyzerBaseUrl: config.douyinRuntimeConfig.videoAnalyzer.baseUrl,
    videoAnalyzerModelId: config.douyinRuntimeConfig.videoAnalyzer.modelId ?? "",
    videoAnalyzerApiKey: "",
    replicationBriefBaseUrl: config.douyinRuntimeConfig.replicationBrief.baseUrl,
    replicationBriefModelId: config.douyinRuntimeConfig.replicationBrief.modelId ?? "",
    replicationBriefApiKey: "",
    vectcutBaseUrl: config.douyinRuntimeConfig.vectcut.baseUrl,
    vectcutFileBaseUrl: config.douyinRuntimeConfig.vectcut.fileBaseUrl,
    vectcutApiKey: ""
  }
}

export function getResetXhsFeishuSettingsDrafts(): XhsFeishuResetSettingsDrafts {
  return {
    xhsFeishuAppId: "",
    xhsFeishuAppSecret: "",
    xhsFeishuAppToken: "",
    xhsFeishuTableId: ""
  }
}

export function getResetFeishuMobileSettingsDrafts(): FeishuMobileResetSettingsDrafts {
  return {
    feishuMobileAppId: "",
    feishuMobileAppSecret: ""
  }
}
