import { createAsyncThunk, createSlice, PayloadAction } from "@reduxjs/toolkit";
import axios from 'axios'
import { fireAlert } from "./alert-slice";

// set the const variables for the slice
const ACTION_NAME = 'statistics'
// slice initial value state
type IinitialState = {
    value: IValueState;
}
// state value type
type IValueState = {
    isLoading: boolean,
    loadedFirstTime: boolean,
    report:
    {
        membersCount: {
            name: string,
            value: number,
        },
        activeSubscriptionsCount: {
            name: "activeSubscriptionsCount",
            value: number,
        },
        expiredSoonSubscriptionsCount: {
            name: "expiredSoonSubscriptionsCount",
            value: number,
        },
        expiredSubscriptionsCount: {
            name: "expiredSubscriptionsCount",
            value: number,
        },
        activeMembersCount: {
            name: "activeMembersCount",
            value: number,
        },
        disActiveMembersCount: {
            name: "disActiveMembersCount",
            value: number,
        },
        servicesCount: {
            name: "servicesCount",
            value: number,
        },
        activeServicesCount: {
            name: "activeServicesCount",
            value: number,
        },
        disActiveServicesCount: {
            name: "disActiveServicesCount",
            value: number,
        },
        membersGeneralOverview: {
            name: "generalMembersOverView",
            value: {
                thisWeek: {
                    'year': number,
                    'month': number,
                    'week': number,
                    'days': {
                        'sat': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'sun': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'mon': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'tue': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'wed': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'thu': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        },
                        'fri': {
                            'totalMembers': number,
                            'totalActiveSubs': number,
                            'totalUnActiveSubs': number,
                            'totalMansMembers': number,
                            'totalGirlsMembers': number,
                        }
                    }
                },
                months: {
                    'year': number,
                    'month': number,
                    'weeks': {
                        'week': number,
                        'days': {
                            'sat': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'sun': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'mon': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'tue': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'wed': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'thu': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'fri': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            }
                        }[]
                    }[]
                }[],
                years: {
                    'year': number,
                    'months': {
                        'month': number,
                        'weeks': {
                            'week': number,
                            'days': {
                                'sat': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'sun': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'mon': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'tue': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'wed': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'thu': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'fri': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                }
                            }[]
                        }[]
                    }[]
                }[],
                thisMonth: {
                    'year': number,
                    'month': number,
                    'weeks': {
                        'week': number,
                        'days': {
                            'sat': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'sun': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'mon': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'tue': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'wed': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'thu': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            },
                            'fri': {
                                'totalMembers': number,
                                'totalActiveSubs': number,
                                'totalUnActiveSubs': number,
                                'totalMansMembers': number,
                                'totalGirlsMembers': number,
                            }
                        }[]
                    }[]
                },
                thisYear: {
                    'year': number,
                    'months': {
                        'month': number,
                        'weeks': {
                            'week': number,
                            'days': {
                                'sat': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'sun': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'mon': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'tue': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'wed': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'thu': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                },
                                'fri': {
                                    'totalMembers': number,
                                    'totalActiveSubs': number,
                                    'totalUnActiveSubs': number,
                                    'totalMansMembers': number,
                                    'totalGirlsMembers': number,
                                }
                            }[]
                        }[]
                    }[]
                }
            },
        },
        servicesNameAndSubscribers: {
            name: "servicesNameAndSubscribers",
            value: {
                _id: string,
                name: string,
                totalSubscribers: number,
            }[]
        },
        workersNameAndJobType: {
            name: "workersNameAndJobType",
            value: {
                _id: string,
                jobType: string,
                count: number,
            }[]
        },
        totalIncome: number,
        totalOutcome: number,
    } | null,
    currentMembersGeneralOverviewDuration: 'thisWeek' | 'thisMonth' | 'thisYear',
    chartSeries: {
        name: string,
        data: number[]
    }[] | [],
    labels: string[] | [],
    preCalculatedCharts: {}
}
// set the initial state
const initialState = {
    value: {
        isLoading: false,
        loadedFirstTime: false,
        report: null,
        currentMembersGeneralOverviewDuration: 'thisWeek',
        chartSeries: [],
        labels: [],
        preCalculatedCharts: {}
    }
} as IinitialState
// thunks
// load data ability
const load = createAsyncThunk(
    'statistics/load',
    async (actionPayload: { forceRefresh?: boolean } = {}, thunkAPI) => {
        try {
            // Add a timestamp to the URL to prevent caching when forceRefresh is true
            const timestamp = actionPayload.forceRefresh ? `?t=${Date.now()}` : '';
            let { data } = await axios.get(`/api/user/actions/${ACTION_NAME}${timestamp}`)
            if (data.success) {
                return data
            } else {
                return { success: false }
            }
        } catch (err) {
            console.error('Error loading statistics:', err);
            return { success: false }
        }
    }
)
// init the slice
export const statistics = createSlice({
    name: ACTION_NAME,
    initialState,
    reducers: {
        setCurrentMembersGeneralOverviewDuration: (state, action: PayloadAction<'thisWeek' | 'thisMonth' | 'thisYear'>) => {
            state.value.currentMembersGeneralOverviewDuration = action.payload
        }
    },
    extraReducers: (builder) => {
        // load ability
        builder.addCase(load.pending, (state: IinitialState, action) => {
            state.value.isLoading = true;
        })
        builder.addCase(load.fulfilled, (state: IinitialState, action) => {
            state.value.isLoading = false;
            if (action.payload?.success && action.payload.data) {
                state.value.loadedFirstTime = true;
                state.value.report = action.payload.data;
            }
        })
        builder.addCase(load.rejected, (state: IinitialState, action) => {
            state.value.isLoading = false;
            console.error('Failed to load statistics:', action.error);
        })
    }
})
// export reducers
export const { setCurrentMembersGeneralOverviewDuration } = statistics.actions;
// export extra reducers
export { load }
// export the slice
export default statistics.reducer;