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

export interface SettingsState {
  lobsterKeyDraft: string;
  xhsFeishuAppIdDraft: string;
  xhsFeishuAppSecretDraft: string;
  xhsFeishuAppTokenDraft: string;
  xhsFeishuTableIdDraft: string;
  workspacePathDraft: string;
  saving: boolean;
}

export function useSettingsState(config: AppConfig | null) {
  const [lobsterKeyDraft, setLobsterKeyDraft] = useState("");
  const [workspacePathDraft, setWorkspacePathDraft] = useState("");
  const [imageModelApiKeyDraft, setImageModelApiKeyDraft] = useState("");
  const [videoModelApiKeyDraft, setVideoModelApiKeyDraft] = useState("");
  const [copywritingModelApiKeyDraft, setCopywritingModelApiKeyDraft] = useState("");
  const [digitalHumanVolcAccessKeyDraft, setDigitalHumanVolcAccessKeyDraft] = useState("");
  const [digitalHumanVolcSecretKeyDraft, setDigitalHumanVolcSecretKeyDraft] = useState("");
  const [digitalHumanQiniuAccessKeyDraft, setDigitalHumanQiniuAccessKeyDraft] = useState("");
  const [digitalHumanQiniuSecretKeyDraft, setDigitalHumanQiniuSecretKeyDraft] = useState("");
  const [videoAnalyzerBaseUrlDraft, setVideoAnalyzerBaseUrlDraft] = useState("");
  const [videoAnalyzerModelIdDraft, setVideoAnalyzerModelIdDraft] = useState("");
  const [videoAnalyzerApiKeyDraft, setVideoAnalyzerApiKeyDraft] = useState("");
  const [replicationBriefBaseUrlDraft, setReplicationBriefBaseUrlDraft] = useState("");
  const [replicationBriefModelIdDraft, setReplicationBriefModelIdDraft] = useState("");
  const [replicationBriefApiKeyDraft, setReplicationBriefApiKeyDraft] = useState("");
  const [vectcutBaseUrlDraft, setVectcutBaseUrlDraft] = useState("");
  const [vectcutFileBaseUrlDraft, setVectcutFileBaseUrlDraft] = useState("");
  const [vectcutApiKeyDraft, setVectcutApiKeyDraft] = useState("");
  const [xhsFeishuAppIdDraft, setXhsFeishuAppIdDraft] = useState("");
  const [xhsFeishuAppSecretDraft, setXhsFeishuAppSecretDraft] = useState("");
  const [xhsFeishuAppTokenDraft, setXhsFeishuAppTokenDraft] = useState("");
  const [xhsFeishuTableIdDraft, setXhsFeishuTableIdDraft] = useState("");
  const [saving, setSaving] = useState(false);

  const hasPendingLobsterKey = lobsterKeyDraft.trim().length > 0;
  const hasPendingXhsFeishuConfig = Boolean(
    xhsFeishuAppIdDraft.trim() ||
    xhsFeishuAppSecretDraft.trim() ||
    xhsFeishuAppTokenDraft.trim() ||
    xhsFeishuTableIdDraft.trim()
  );
  const hasPendingModelKeys = Boolean(
    imageModelApiKeyDraft.trim()
    || videoModelApiKeyDraft.trim()
    || copywritingModelApiKeyDraft.trim()
    || digitalHumanVolcAccessKeyDraft.trim()
    || digitalHumanVolcSecretKeyDraft.trim()
    || digitalHumanQiniuAccessKeyDraft.trim()
    || digitalHumanQiniuSecretKeyDraft.trim()
    || videoAnalyzerBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.videoAnalyzer.baseUrl ?? "").trim()
    || videoAnalyzerModelIdDraft.trim() !== (config?.douyinRuntimeConfig.videoAnalyzer.modelId ?? "").trim()
    || videoAnalyzerApiKeyDraft.trim()
    || replicationBriefBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.replicationBrief.baseUrl ?? "").trim()
    || replicationBriefModelIdDraft.trim() !== (config?.douyinRuntimeConfig.replicationBrief.modelId ?? "").trim()
    || replicationBriefApiKeyDraft.trim()
    || vectcutBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.vectcut.baseUrl ?? "").trim()
    || vectcutFileBaseUrlDraft.trim() !== (config?.douyinRuntimeConfig.vectcut.fileBaseUrl ?? "").trim()
    || vectcutApiKeyDraft.trim()
  );

  return {
    lobsterKeyDraft,
    setLobsterKeyDraft,
    workspacePathDraft,
    setWorkspacePathDraft,
    imageModelApiKeyDraft,
    setImageModelApiKeyDraft,
    videoModelApiKeyDraft,
    setVideoModelApiKeyDraft,
    copywritingModelApiKeyDraft,
    setCopywritingModelApiKeyDraft,
    digitalHumanVolcAccessKeyDraft,
    setDigitalHumanVolcAccessKeyDraft,
    digitalHumanVolcSecretKeyDraft,
    setDigitalHumanVolcSecretKeyDraft,
    digitalHumanQiniuAccessKeyDraft,
    setDigitalHumanQiniuAccessKeyDraft,
    digitalHumanQiniuSecretKeyDraft,
    setDigitalHumanQiniuSecretKeyDraft,
    videoAnalyzerBaseUrlDraft,
    setVideoAnalyzerBaseUrlDraft,
    videoAnalyzerModelIdDraft,
    setVideoAnalyzerModelIdDraft,
    videoAnalyzerApiKeyDraft,
    setVideoAnalyzerApiKeyDraft,
    replicationBriefBaseUrlDraft,
    setReplicationBriefBaseUrlDraft,
    replicationBriefModelIdDraft,
    setReplicationBriefModelIdDraft,
    replicationBriefApiKeyDraft,
    setReplicationBriefApiKeyDraft,
    vectcutBaseUrlDraft,
    setVectcutBaseUrlDraft,
    vectcutFileBaseUrlDraft,
    setVectcutFileBaseUrlDraft,
    vectcutApiKeyDraft,
    setVectcutApiKeyDraft,
    xhsFeishuAppIdDraft,
    setXhsFeishuAppIdDraft,
    xhsFeishuAppSecretDraft,
    setXhsFeishuAppSecretDraft,
    xhsFeishuAppTokenDraft,
    setXhsFeishuAppTokenDraft,
    xhsFeishuTableIdDraft,
    setXhsFeishuTableIdDraft,
    saving,
    setSaving,
    hasPendingLobsterKey,
    hasPendingXhsFeishuConfig,
    hasPendingModelKeys
  };
}
