import { create } from 'zustand';
import { message } from 'antd';
import { fetchAuditList, fetchAuditStats, auditAction } from '@/apis';

interface PageState {
    pageNum: number;
    pageSize: number;
    total: number;
}

interface AuditState {
    query: any;
    listLoading: boolean;
    list: any[];
    page: PageState;
    infoModalId: string;
    infoModalOpen: boolean;
    stats: {
        total: number;
        pending: number;
        approved: number;
        rejected: number;
    } | null;
}

interface AuditActions {
    setQuery: (query: any) => void;
    setListLoading: (loading: boolean) => void;
    setList: (list: any[]) => void;
    setPage: (page: PageState) => void;
    setInfoModalId: (id: string) => void;
    setInfoModalOpen: (open: boolean) => void;
    setStats: (stats: any) => void;
}

export type AuditStore = AuditState & AuditActions & {
    fetchAuditList: () => Promise<void>;
    fetchAuditStats: () => Promise<void>;
    onAuditPass: (auditId: string, opinion: string) => Promise<void>;
    onAuditReject: (auditId: string, opinion: string) => Promise<void>;
};

const initialState: AuditState = {
    query: undefined,
    listLoading: false,
    list: [],
    page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
    },
    infoModalId: '',
    infoModalOpen: false,
    stats: null,
};

export const useAuditStore = create<AuditStore>((set, get) => ({
    ...initialState,

    setQuery: (query) => set({ query }),

    setListLoading: (loading) => set({ listLoading: loading }),

    setList: (list) => set({ list }),

    setPage: (page) => set({ page }),

    setInfoModalId: (id) => set({ infoModalId: id }),

    setInfoModalOpen: (open) => set({ infoModalOpen: open }),

    setStats: (stats) => set({ stats }),

    fetchAuditList: async () => {
        set({ listLoading: true });
        try {
            const { query, page } = get();
            const res: any = await fetchAuditList({
                ...query,
                pageNum: page.pageNum,
                pageSize: page.pageSize,
            });

            if (res.code === 200) {
                set({
                    list: res.data?.list || [],
                    page: {
                        ...page,
                        total: res.data?.total || 0,
                    },
                });
            }
        } catch (error) {
            console.error('获取审核列表失败:', error);
            message.error('获取审核列表失败');
        } finally {
            set({ listLoading: false });
        }
    },

    fetchAuditStats: async () => {
        try {
            const res: any = await fetchAuditStats();
            if (res.code === 200) {
                set({ stats: res.data || null });
            }
        } catch (error) {
            console.error('获取统计数据失败:', error);
        }
    },

    onAuditPass: async (auditId: string, opinion: string) => {
        try {
            const res: any = await auditAction({
                auditId,
                action: 'approve',
                opinion,
            });
            if (res.code === 200) {
                message.success('审核通过');
                get().fetchAuditList();
                get().fetchAuditStats();
            }
        } catch (error) {
            message.error('审核失败');
        }
    },

    onAuditReject: async (auditId: string, opinion: string) => {
        try {
            const res: any = await auditAction({
                auditId,
                action: 'reject',
                opinion,
            });
            if (res.code === 200) {
                message.success('已拒绝');
                get().fetchAuditList();
                get().fetchAuditStats();
            }
        } catch (error) {
            message.error('操作失败');
        }
    },
}));

export default useAuditStore;
