// ============================================================
// SettingsProvider — 设置项集中状态管理 Context + Provider + Hook
//
// 职责：
//   - 存储路径的集中管理（read/write/clear）
//   - 持久化到 localStorage，遵循 ele-heixiu-* 键名规范
//   - 全局组件通过 useSettings() 消费
// ============================================================

import { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
import { setOutputPath as saveOutputPath, setTeamRepoPath as saveTeamRepoPath } from '@/shared/infrastructure/storage';

// ---------- localStorage 键 ----------

const OUTPUT_PATH_KEY = 'ele-heixiu-output-path';
const TEAM_REPO_PATH_KEY = 'ele-heixiu-team-repo-path';

// ---------- Context 类型 ----------

export interface SettingsContextValue {
   /** 大模型产物存储仓库路径 */
   outputPath: string;
   /** 团队创作存储仓库路径（企业版） */
   teamRepoPath: string;
   /** 设置产物输出路径 */
   setOutputPath: (path: string) => void;
   /** 设置团队创作仓库路径 */
   setTeamRepoPath: (path: string) => void;
   /** 清除产物输出路径 */
   clearOutputPath: () => void;
   /** 清除团队创作仓库路径 */
   clearTeamRepoPath: () => void;
}

export const SettingsContext = createContext<SettingsContextValue | null>(null);

// ---------- 工具函数（localStorage 读写）----------

export function readStoredOutputPath(): string {
   try {
      return localStorage.getItem(OUTPUT_PATH_KEY) || '';
   } catch {
      return '';
   }
}

export function writeStoredOutputPath(path: string): void {
   try {
      localStorage.setItem(OUTPUT_PATH_KEY, path);
   } catch {
      /* 静默忽略 */
   }
}

export function clearStoredOutputPath(): void {
   try {
      localStorage.removeItem(OUTPUT_PATH_KEY);
   } catch {
      /* 静默忽略 */
   }
}

export function readStoredTeamRepoPath(): string {
   try {
      return localStorage.getItem(TEAM_REPO_PATH_KEY) || '';
   } catch {
      return '';
   }
}

export function writeStoredTeamRepoPath(path: string): void {
   try {
      localStorage.setItem(TEAM_REPO_PATH_KEY, path);
   } catch {
      /* 静默忽略 */
   }
}

export function clearStoredTeamRepoPath(): void {
   try {
      localStorage.removeItem(TEAM_REPO_PATH_KEY);
   } catch {
      /* 静默忽略 */
   }
}

// ---------- Consumer Hook ----------

export function useSettings(): SettingsContextValue {
   const ctx = useContext(SettingsContext);
   if (!ctx) {
      throw new Error('useSettings() 必须在 <SettingsProvider> 内部调用');
   }
   return ctx;
}

// ---------- Provider 组件 ----------

interface SettingsProviderProps {
   children: ReactNode;
}

export function SettingsProvider({ children }: SettingsProviderProps) {
   const [outputPath, setOutputPathState] = useState<string>(readStoredOutputPath);
   const [teamRepoPath, setTeamRepoPathState] = useState<string>(readStoredTeamRepoPath);

   // ---------- outputPath ----------

   const setOutputPath = useCallback((path: string) => {
      setOutputPathState(path);
      writeStoredOutputPath(path);
      try { saveOutputPath(path); } catch { /* db 未初始化则跳过 */ }
   }, []);

   const clearOutputPath = useCallback(() => {
      setOutputPathState('');
      clearStoredOutputPath();
   }, []);

   // ---------- teamRepoPath ----------

   const setTeamRepoPath = useCallback((path: string) => {
      setTeamRepoPathState(path);
      writeStoredTeamRepoPath(path);
      try { saveTeamRepoPath(path); } catch { /* db 未初始化则跳过 */ }
   }, []);

   const clearTeamRepoPath = useCallback(() => {
      setTeamRepoPathState('');
      clearStoredTeamRepoPath();
   }, []);

   const value: SettingsContextValue = {
      outputPath,
      teamRepoPath,
      setOutputPath,
      setTeamRepoPath,
      clearOutputPath,
      clearTeamRepoPath,
   };

   return <SettingsContext.Provider value={value}>{children}</SettingsContext.Provider>;
}
