import { create } from 'zustand';
import { message } from 'antd';
import { apis } from '@/apis';

interface PageState {
    page: number;
    size: number;
    total: number;
}

interface KnowledgeDetailState {
    knowledge_id: string;
    name: string;
    listLoading: boolean;
    list: any[];
    processingList: any[];
    infoModalId: string;
    infoModalOpen: boolean;
    infoModalSettingId: string;
    infoModalSettingOpen: boolean;
    page: PageState;
    knowledgeDetail: any;
}

interface KnowledgeDetailActions {
    setknowledge_id: (id: string) => void;
    setListLoading: (loading: boolean) => void;
    setList: (list: any[]) => void;
    setProcessingList: (list: any[]) => void;
    setInfoModalId: (id: string) => void;
    setInfoModalOpen: (open: boolean) => void;
    setPage: (page: PageState) => void;
    setInfoModalSettingOpen: (open: boolean) => void;
    setInfoModalSettingId: (id: string) => void;
    setKnowledgeDetail: (detail: any) => void;
    setName: (name: string) => void;
}

export type KnowledgeDetailStore = KnowledgeDetailState & KnowledgeDetailActions & {
    fetchKnowledgeDetail: () => Promise<void>;
    fetchDocumentList: () => Promise<void>;
};

const initialState: KnowledgeDetailState = {
    knowledge_id: '',
    name: '',
    listLoading: false,
    list: [],
    processingList: [],
    infoModalId: '',
    infoModalOpen: false,
    infoModalSettingId: '',
    infoModalSettingOpen: false,
    page: {
        page: 1,
        size: 10,
        total: 0,
    },
    knowledgeDetail: null,
};

export const useKnowledgeDetailStore = create<KnowledgeDetailStore>((set, get) => ({
    ...initialState,
    
    setknowledge_id: (id) => set({ knowledge_id: id }),
    
    setListLoading: (loading) => set({ listLoading: loading }),
    
    setList: (list) => set({ list }),
    
    setProcessingList: (list) => set({ processingList: list }),
    
    setInfoModalId: (id) => set({ infoModalId: id }),
    
    setInfoModalOpen: (open) => set({ infoModalOpen: open }),
    
    setPage: (page) => set({ page }),
    
    setInfoModalSettingOpen: (open) => set({ infoModalSettingOpen: open }),
    
    setInfoModalSettingId: (id) => set({ infoModalSettingId: id }),
    
    setKnowledgeDetail: (detail) => set({ knowledgeDetail: detail }),
    
    setName: (name) => set({ name }),
    
    fetchKnowledgeDetail: async () => {
        set({ listLoading: true });
        try {
            const { knowledge_id } = get();
            const res: any = await apis.fetchKnowledgeDetail(knowledge_id);
            if (res.code === 200) {
                set({ knowledgeDetail: res.data });
            }
        } catch (error) {
            console.error('获取知识库详情失败:', error);
        } finally {
            set({ listLoading: false });
        }
    },
    
    fetchDocumentList: async () => {
        set({ listLoading: true });
        try {
            const { knowledge_id, page } = get();
            const res: any = await apis.fetchDocumentList({
                knowledge_id,
                page: page.page,
                size: page.size,
            });
            
            if (res.code === 200) {
                set({
                    list: res.rows || [],
                    page: {
                        ...page,
                        total: res.total || 0,
                    },
                });
            }
        } catch (error) {
            console.error('获取文档列表失败:', error);
        } finally {
            set({ listLoading: false });
        }
    },
}));

export default useKnowledgeDetailStore;
