import { createSlice, createSelector } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";

interface IChatsAnswers {
    psychicReading: string;
    goal: string;
    relationshipStatus: string;
    satisfiedWithLife: string;
    parentStatus: string;
    whatEmotion: string;
    whatYouWant: string;
    missingInLife: string;
    yourGoal: string;
    somethingWorrying: string;
    proneToOverthinking: string;
    worriesImpact: string;
    sensitiveToCriticism: string;
    headOrHeart: string;
    howConfident: string;
    needGuidance: string;
    decisionGuidance: string;
    resonateLove: string;
    resonateLife: string;
    resonateFuture: string;
    believeInSpirituality: string;
    readingExperience: string;
    interestedHowLong: string;
    mainReason: string;
    whatToExpect: string;
    guidanceArea: string[];
    psychicComfortable: string[];
    userName: string;
}

interface IChats {
    currentStep: number;
    answers: IChatsAnswers;
}

const initialState: IChats = {
    currentStep: 0,
    answers: {
        psychicReading: "",
        goal: "",
        relationshipStatus: "",
        satisfiedWithLife: "",
        parentStatus: "",
        whatEmotion: "",
        whatYouWant: "",
        missingInLife: "",
        yourGoal: "",
        somethingWorrying: "",
        proneToOverthinking: "",
        worriesImpact: "",
        sensitiveToCriticism: "",
        headOrHeart: "",
        howConfident: "",
        needGuidance: "",
        decisionGuidance: "",
        resonateLove: "",
        resonateLife: "",
        resonateFuture: "",
        believeInSpirituality: "",
        readingExperience: "",
        interestedHowLong: "",
        mainReason: "",
        whatToExpect: "",
        guidanceArea: [],
        psychicComfortable: [],
        userName: "",
    }
};

const chatsSlice = createSlice({
    name: "chats",
    initialState,
    reducers: {
        update(state, action: PayloadAction<Partial<IChats>>) {
            return { ...state, ...action.payload };
        },
        updateCurrentStep(state, action: PayloadAction<number>) {
            return { ...state, currentStep: action.payload };
        },
        updateAnswers(state, action: PayloadAction<Partial<IChatsAnswers>>) {
            return {
                ...state, answers: {
                    ...state.answers,
                    ...action.payload
                }
            };
        },
    },
    extraReducers: (builder) => builder.addCase("reset", () => initialState),
});

export const { actions } = chatsSlice;

export const selectCurrentStep = createSelector(
    (state: { chats: IChats }) => state.chats.currentStep,
    (chats) => chats
);

export const selectAnswers = createSelector(
    (state: { chats: IChats }) => state.chats.answers,
    (chats) => chats
);

export default chatsSlice.reducer;
