/**
 * File: authenticationSlice.ts
 * Description:
 * - This file defines the Redux slice for managing authentication-related state in the application.
 * - It uses Redux Toolkit's `createSlice` to simplify state management and reduce boilerplate code.
 * - The slice contains actions and reducers for handling various authentication scenarios such as sign-in, sign-up, OTP verification, password reset, etc.
*/


import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { setEncryptedCookie } from '@/src/utils/commonFunctions';
import { AUTH_COOKIES } from '@/src/utils/commonVariables';

interface AuthenticationState {
    isLoading: boolean;
    userData: any;
    accessToken: string;
    emailLRFData: string
}
const initialState: AuthenticationState = {
    isLoading: false,
    userData: null,
    emailLRFData: null,
    accessToken: null,
}

const authentication = createSlice({
    name: 'authentication',
    initialState,
    reducers: {
        /**
         * Used to start the request of signin api call
         * @param state: State is the object data of credential passed
         */
        requestSignIn: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the signin api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of signin api
         */
        responseSignIn: (state: AuthenticationState, action: PayloadAction<any>) => {
            const { data, meta } = action.payload
            if (meta?.token) setEncryptedCookie(AUTH_COOKIES.USER_TOKEN, meta.token)
            return {
                ...state,
                isLoading: false,
                userData: data,
                accessToken: meta?.token ? meta.token : null
            };
        },
        /**
         * Used to get error for the signin api call
         * @param state: State is the object data of credential passed
         */
        errorSignIn: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },


        /**
         * Used to start the request of signup api call
         * @param state: State is the object data of credential passed
         */
        requestSignUp: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the signup api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of signup api
         */
        responseSignUp: (state: AuthenticationState, action: PayloadAction<any>) => {
            return {
                ...state,
                isLoading: false,
                emailLRFData: action.payload?.data?.email
            };
        },
        /**
         * Used to get error for the signup api call
         * @param state: State is the object data of credential passed
         */
        errorSignUp: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to start the request of username availability api call
         * @param state: State is the object data of credential passed
         */
        requestCheckUserName: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the username availability api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of username availability api          */
        responseCheckUserName: (state: AuthenticationState) => {
            return {
                ...state,
                isLoading: false,
            };
        },
        /**
         * Used to get error for the username availability api call
         * @param state: State is the object data of credential passed
         */
        errorCheckUserName: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to start the request of verify otp api call
         * @param state: State is the object data of credential passed
         */
        requestVerifyOTP: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the verify otp api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of verify otp api
         */
        responseVerifyOTP: (state: AuthenticationState, action: PayloadAction<any>) => {
            const { data, meta } = action.payload
            if (meta?.token) setEncryptedCookie(AUTH_COOKIES.USER_TOKEN, meta.token)
            return {
                ...state,
                isLoading: false,
                userData: data,
                accessToken: meta?.token ? meta.token : null
            };
        },
        /**
         * Used to get error for the verify otp api call
         * @param state: State is the object data of credential passed
         */
        errorVerifyOTP: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to start the request of verify otp api call
         * @param state: State is the object data of credential passed
         */
        requestResendOTP: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the verify otp api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of verify otp api
         */
        responseResendOTP: (state: AuthenticationState, action: PayloadAction<any>) => {
            return {
                ...state,
                isLoading: false,
            };
        },
        /**
         * Used to get error for the verify otp api call
         * @param state: State is the object data of credential passed
         */
        errorResendOTP: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to start the request of forgot password api call
         * @param state: State is the object data of credential passed
         */
        requestForgotPassword: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the forgot password api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of forgot password api
         */
        responseForgotPassword: (state: AuthenticationState) => {
            return {
                ...state,
                isLoading: false,
            };
        },
        /**
         * Used to get error for the forgot password api call
         * @param state: State is the object data of credential passed
         */
        errorForgotPassword: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to start the request of reset password api call
         * @param state: State is the object data of credential passed
         */
        requestResetPassword: (state: AuthenticationState) => {
            return { ...state, isLoading: true };
        },
        /**
         * Used to get the actual success response of the reset password api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of reset password api          */
        responseResetPassword: (state: AuthenticationState) => {
            return {
                ...state,
                isLoading: false,
            };
        },
        /**
         * Used to get error for the reset password api call
         * @param state: State is the object data of credential passed
         */
        errorResetPassword: (state: AuthenticationState) => {
            return { ...state, isLoading: false };
        },

        /**
         * Used to get the actual success response of the reset password api
         * @param state: State is the object data of credential passed
         * @param action: action is used to store the data which is set on success of reset password api
         */
        responseEmailLRFData: (state: AuthenticationState, action: PayloadAction<any>) => {
            return {
                ...state,
                emailLRFData: action.payload,
            };
        },
    },
});

export const authActions = authentication.actions
export default authentication.reducer;