import { create } from 'zustand';

interface FormState {
    // 表单实例（在组件中创建）
    form: any;
    
    // 步骤
    step: number;
    
    // 加载状态
    pageLoading: boolean;
    
    // 数据列表
    appTypeList: any[];
    appVisibleList: any[];
    appProjectList: any[];
    knowledgeList: any[];
    modelList: any[];
    rerankModelList: any[];
    userList: any[];
    
    // 状态标志
    isAppPro: boolean;
    visibleFlag: string | number;
    vipList: any[];
    
    // 参数值
    topPValue: number;
    tempValue: number;
    
    // 可见性标志
    isVisible: boolean;
    isVisibleCus: boolean;
    isVisibleSlice: boolean;
    isDeepThinkVisible: boolean;
    isMultiRound: boolean;
    automatic: boolean;
    
    // 应用信息
    name: string;
    appId?: string;
    createFlag: boolean;
    
    // 图标相关
    inputs: Array<{ id: number; value: string }>;
    selectedIcon: string | null;
    previewBg: string;
}

interface FormActions {
    // 设置器
    setForm: (form: any) => void;
    setStep: (step: number) => void;
    setPageLoading: (loading: boolean) => void;
    setAppTypeList: (list: any[]) => void;
    setAppVisibleList: (list: any[]) => void;
    setAppProjectList: (list: any[]) => void;
    setKnowledgeList: (list: any[]) => void;
    setModelList: (list: any[]) => void;
    setRerankModelList: (list: any[]) => void;
    setUserList: (list: any[]) => void;
    setIsAppPro: (flag: boolean) => void;
    setVisibleFlag: (flag: string | number) => void;
    setVipList: (list: any[]) => void;
    setTopPValue: (value: number) => void;
    setTempValue: (value: number) => void;
    setIsVisible: (flag: boolean) => void;
    setIsVisibleCus: (flag: boolean) => void;
    setIsVisibleSlice: (flag: boolean) => void;
    setIsDeepThinkVisible: (flag: boolean) => void;
    setIsMultiRound: (flag: boolean) => void;
    setAutomatic: (flag: boolean) => void;
    setName: (name: string) => void;
    setAppId: (id?: string) => void;
    setCreateFlag: (flag: boolean) => void;
    setInputs: (inputs: Array<{ id: number; value: string }>) => void;
    setSelectedIcon: (icon: string | null) => void;
    setPreviewBg: (color: string) => void;
    
    // 重置
    reset: () => void;
}

export type QuestionAnswerFormStore = FormState & FormActions;

const initialState: FormState = {
    form: null,  // 在组件中通过 Form.useForm() 创建
    step: 1,
    pageLoading: false,
    appTypeList: [],
    appVisibleList: [],
    appProjectList: [],
    knowledgeList: [],
    modelList: [],
    rerankModelList: [],
    userList: [],
    isAppPro: false,
    visibleFlag: '0',
    vipList: [],
    topPValue: 0.5,  // 默认值
    tempValue: 0.5,  // 默认值
    isVisible: false,
    isVisibleCus: false,
    isVisibleSlice: false,
    isDeepThinkVisible: false,
    isMultiRound: false,
    automatic: false,
    name: '',
    appId: undefined,
    createFlag: false,
    inputs: [{ id: 1, value: '' }],
    selectedIcon: null,
    previewBg: '#005D80',  // 默认企业蓝色
};

export const useQuestionAnswerFormStore = create<QuestionAnswerFormStore>((set) => ({
    ...initialState,
    
    setForm: (form) => set({ form }),
    setStep: (step) => set({ step }),
    setPageLoading: (loading) => set({ pageLoading: loading }),
    setAppTypeList: (list) => set({ appTypeList: list }),
    setAppVisibleList: (list) => set({ appVisibleList: list }),
    setAppProjectList: (list) => set({ appProjectList: list }),
    setKnowledgeList: (list) => set({ knowledgeList: list }),
    setModelList: (list) => set({ modelList: list }),
    setRerankModelList: (list) => set({ rerankModelList: list }),
    setUserList: (list) => set({ userList: list }),
    setIsAppPro: (flag) => set({ isAppPro: flag }),
    setVisibleFlag: (flag) => set({ visibleFlag: flag }),
    setVipList: (list) => set({ vipList: list }),
    setTopPValue: (value) => set({ topPValue: value }),
    setTempValue: (value) => set({ tempValue: value }),
    setIsVisible: (flag) => set({ isVisible: flag }),
    setIsVisibleCus: (flag) => set({ isVisibleCus: flag }),
    setIsVisibleSlice: (flag) => set({ isVisibleSlice: flag }),
    setIsDeepThinkVisible: (flag) => set({ isDeepThinkVisible: flag }),
    setIsMultiRound: (flag) => set({ isMultiRound: flag }),
    setAutomatic: (flag) => set({ automatic: flag }),
    setName: (name) => set({ name }),
    setAppId: (id) => set({ appId: id }),
    setCreateFlag: (flag) => set({ createFlag: flag }),
    setInputs: (inputs) => set({ inputs }),
    setSelectedIcon: (icon) => set({ selectedIcon: icon }),
    setPreviewBg: (color) => set({ previewBg: color }),
    
    reset: () => set(initialState),
}));

export default useQuestionAnswerFormStore;
