import { create } from "zustand";
import { persist } from "zustand/middleware";

export interface AIPrompt {
    id: string;
    label: string;
    description: string;
    systemPrompt: string;
}

export const defaultPrompts: AIPrompt[] = [
    {
        id: "improve",
        label: "润色内容",
        description: "提升语句表达质量",
        systemPrompt:
            "你是专业的写作润色助手。请优化用户提供的文本，使其更清晰、流畅、专业，并保持原意。请直接输出优化后的文本。",
    },
    {
        id: "complete",
        label: "续写内容",
        description: "基于上下文自然续写",
        systemPrompt:
            "你是富有创意的写作助手。请根据用户提供的上下文自然续写，保证语气和风格一致。",
    },
    {
        id: "summarize",
        label: "生成摘要",
        description: "提取核心观点",
        systemPrompt:
            "你是专业的文档总结助手。请对用户文本进行简明摘要，提取关键要点，使用中文回答。",
    },
];

interface SettingsState {
    fontFamily: string;
    fontSize: number;
    lineHeight: number;
    tableLineHeight: number;
    timezone: string;
    localHistory: {
        maxSnapshotsPerPage: number;
        retentionDays: number;
    };

    aiConfig: {
        apiKey: string;
        baseURL: string;
        model: string;
    };

    prompts: AIPrompt[];

    setFontFamily: (font: string) => void;
    setFontSize: (size: number) => void;
    setLineHeight: (height: number) => void;
    setTableLineHeight: (height: number) => void;
    setTimezone: (timezone: string) => void;
    setLocalHistory: (config: Partial<{ maxSnapshotsPerPage: number; retentionDays: number }>) => void;
    setAIConfig: (config: Partial<{ apiKey: string; baseURL: string; model: string }>) => void;

    setPrompts: (prompts: AIPrompt[]) => void;
    addPrompt: (prompt: AIPrompt) => void;
    updatePrompt: (id: string, prompt: Partial<AIPrompt>) => void;
    deletePrompt: (id: string) => void;
    resetPrompts: () => void;
}

export const useSettingsStore = create<SettingsState>()(
    persist(
        (set) => ({
            fontFamily: "Inter",
            fontSize: 16,
            lineHeight: 1.5,
            tableLineHeight: 1.2,
            timezone: "Asia/Shanghai",
            localHistory: {
                maxSnapshotsPerPage: 30,
                retentionDays: 30,
            },

            aiConfig: {
                apiKey: "",
                baseURL: "https://api.openai.com/v1",
                model: "gpt-3.5-turbo",
            },

            prompts: defaultPrompts,

            setFontFamily: (font) => set({ fontFamily: font }),
            setFontSize: (size) => set({ fontSize: size }),
            setLineHeight: (height) => set({ lineHeight: height }),
            setTableLineHeight: (height) => set({ tableLineHeight: height }),
            setTimezone: (timezone) => set({ timezone }),
            setLocalHistory: (config) =>
                set((state) => ({
                    localHistory: {
                        ...state.localHistory,
                        ...config,
                    },
                })),
            setAIConfig: (config) =>
                set((state) => ({
                    aiConfig: {
                        ...state.aiConfig,
                        ...config,
                    },
                })),

            setPrompts: (prompts) => set({ prompts }),
            addPrompt: (prompt) => set((state) => ({ prompts: [...state.prompts, prompt] })),
            updatePrompt: (id, prompt) =>
                set((state) => ({
                    prompts: state.prompts.map((p) => (p.id === id ? { ...p, ...prompt } : p)),
                })),
            deletePrompt: (id) => set((state) => ({ prompts: state.prompts.filter((p) => p.id !== id) })),
            resetPrompts: () => set({ prompts: defaultPrompts }),
        }),
        {
            name: "noteai-settings",
            partialize: (state) => ({
                fontFamily: state.fontFamily,
                fontSize: state.fontSize,
                lineHeight: state.lineHeight,
                tableLineHeight: state.tableLineHeight,
                timezone: state.timezone,
                localHistory: state.localHistory,
                aiConfig: {
                    baseURL: state.aiConfig.baseURL,
                    model: state.aiConfig.model,
                    apiKey: "",
                },
                prompts: state.prompts,
            }),
        }
    )
);

export const fontOptions = [
    { label: "Default (Inter)", value: "Inter" },
    { label: "Serif", value: "serif" },
    { label: "Mono", value: "monospace" },
    { label: "System", value: "system-ui" },
];

export const timezoneOptions = [
    { label: "UTC", value: "UTC" },
    { label: "Shanghai (UTC+8)", value: "Asia/Shanghai" },
    { label: "Tokyo (UTC+9)", value: "Asia/Tokyo" },
    { label: "New York (UTC-5/EDT)", value: "America/New_York" },
    { label: "London (UTC+0/BST)", value: "Europe/London" },
    { label: "Paris (UTC+1/CEST)", value: "Europe/Paris" },
    { label: "Sydney (UTC+10/AEST)", value: "Australia/Sydney" },
    { label: "Dubai (UTC+4)", value: "Asia/Dubai" },
    { label: "Los Angeles (UTC-8/PDT)", value: "America/Los_Angeles" },
];
