import { makeAutoObservable, set } from 'mobx';
import { message } from 'antd';
import { apis, LoginApiParams } from '@/apis';
import { state } from '@antv/g2plot/lib/adaptor/common';
import { c } from 'node_modules/vite/dist/node/moduleRunnerTransport.d-DJ_mE5sf';


// 定义状态 
const stateGenerator = () => ({
    monthDay: 'day',
    topData: {
        todayNum: 0,
        totalNum: 0,
        successRate: 0,
        bjNum: 0,
        bjYNum:0
    },
    onlineUserData: [], // 活跃用户数
    conversationData: [], // 会话数据
    conversationDetailData: [], // 互动数据
    overviewTokenData: [], // token数据

});

// 修改状态
const stateActionsGenerator = (state: any) => {
    return {
        setMonthDay: (monthDay: string) => {
            state.monthDay = monthDay;
        },
        setTopData: (topData: any) => {
            state.topData = { ...topData };
        },
        setOnlineUserData: (onlineUserData: any) => {
            state.onlineUserData = [...onlineUserData];
        },
        setConversationData: (conversationData: any) => {
            state.conversationData = [...conversationData];
        },
        setConversationDetailData: (conversationDetailData: any) => {
            state.conversationDetailData = [...conversationDetailData];
        },
        setOverviewTokenData: (overviewTokenData: any) => {
            state.overviewTokenData = [...overviewTokenData];
        },
    }
}
// 使用仓库

const useHomeStore = () => {
    const state = makeAutoObservable(stateGenerator());
    const actions = stateActionsGenerator(state);

    const onFetchTopData = async () => {
        const res: any = await apis.fetchTopData()
        if (res.code === 200) {
            actions.setTopData(res.data)
        }
    }
    const onSetMonthDay = (e: any) => {
        actions.setMonthDay(e)
    }
    const onOverviewData = async () => {
        const now = new Date();
        // 核心：getMonth()返回0-11，+1得到实际月份（1-12）
        const currentMonth = now.getMonth() + 1;
        const currentYear = now.getFullYear();

        const resUser: any = await apis.fetchOnlineUserData({ monthDay: state.monthDay });
        if (resUser.code === 200) {
            const flagData = resUser.data?.map((item: any, index: number) => {
                const currentTime = item.createTime.split(' ')[0].split('-')
                return {
                    [state.monthDay]: state.monthDay === 'day' ? `${currentTime[1]}-${currentTime[2]}` : `${currentTime[0]}-${currentTime[1]}`,
                    value: Number(item.onlineNum),
                }
            })
            actions.setOnlineUserData(flagData || [])
        }
        const resConer: any = await apis.fetchConversationData({ monthDay: state.monthDay });
        if (resConer.code === 200) {
            const flagData = resConer.data?.map((item: any, index: number) => {
                const currentTime = item.createTime.split(' ')[0].split('-')
                return {
                    [state.monthDay]: state.monthDay === 'day' ? `${currentTime[1]}-${currentTime[2]}` : item.createTime,
                    value: Number(item.conversationNum),
                }
            })
            actions.setConversationData(flagData || [])
        }
        const resConver: any = await apis.fetchConversationDetailData({ monthDay: state.monthDay });
        if (resConver.code === 200) {
            const flagData = resConver.data?.map((item: any, index: number) => {
                const currentTime = item.createTime.split(' ')[0].split('-')
                return {
                    [state.monthDay]: state.monthDay === 'day' ? `${currentTime[1]}-${currentTime[2]}` : item.createTime,
                    value: Number(item.conversationNum),
                }
            })
            actions.setConversationDetailData(flagData || [])
        }
        const resToken: any = await apis.fetchOverviewTokenData({ monthDay: state.monthDay });
        if (resToken.code === 200) {
            const flagData = resToken.data?.map((item: any, index: number) => {
                const currentTime = item.createTime.split(' ')[0].split('-')
                return {
                    [state.monthDay]: state.monthDay === 'day' ? `${currentTime[1]}-${currentTime[2]}` : `${currentTime[0]}-${currentTime[1]}`,
                    value: Number(item.tokenSpeed),
                }
            })
            actions.setOverviewTokenData(flagData || [])
        }
    }
    const init = () => {
        onFetchTopData()
        onOverviewData()
    }



    return {
        state,
        init,
        onSetMonthDay,
        onOverviewData
    }
}

export default useHomeStore();