/**
 * File: bookingSlice.ts
 * Description:
 * - This file defines the Redux slice for managing booking-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 booking scenarios such as booking, etc.
*/
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface bookingState {
    isLoading: boolean;
    viewPropertyAvailability: any;
    bookingSummary: any;
    isbookingSummaryLoading: boolean
    createBookingData: any;
    isCreateBookingLoading: boolean;
    viewBookingData: any;
    isViewBookingLoading: boolean;
    paymentDetailsList: any;
    isPaymentLoading: boolean;
    paymentDetailsAddEdit: any;
    isPaymentAddEditLoading: boolean;
    isCancelBookingLoading: boolean;
    isRaiseDisputeLoading: boolean
}

const initialState: bookingState = {
    isLoading: false,
    viewPropertyAvailability: null,
    bookingSummary: null,
    isbookingSummaryLoading: true,
    createBookingData: null,
    isCreateBookingLoading: false,
    viewBookingData: null,
    isViewBookingLoading: false,
    paymentDetailsList: null,
    isPaymentLoading: false,
    paymentDetailsAddEdit: null,
    isPaymentAddEditLoading: false,
    isCancelBookingLoading: false,
    isRaiseDisputeLoading: false,
}

const bookingSlice = createSlice({
    name: 'booking',
    initialState,
    reducers: {
        /**
         * Used to start the request of booking list api call
         * @param state: State is the object data of credential passed
         */
        requestViewPropertyAvailability: (state) => {
            state.isLoading = true;
        },
        /**
         * Used to get the actual success response of the booking list api
         * @param state: State is the object data of credential passed
         */
        successViewPropertyAvailability: (state, action: PayloadAction<any>) => {
            state.viewPropertyAvailability = action.payload;
            state.isLoading = false;
        },
        /**
         * Used to get the error response of the booking list api
         * @param state: State is the object data of credential passed
         */
        failureViewPropertyAvailability: (state) => {
            state.isLoading = false;
            state.viewPropertyAvailability = null;
        },

        /**
         * Used to start the request of booking list api call
         * @param state: State is the object data of credential passed
         */
        requestBookingSummary: (state) => {
            state.isbookingSummaryLoading = true;
            state.bookingSummary = null;
        },
        /**
         * Used to get the actual success response of the booking list api
         * @param state: State is the object data of credential passed
         */
        successBookingSummary: (state, action: PayloadAction<any>) => {
            state.bookingSummary = action.payload;
            state.isbookingSummaryLoading = false;
        },
        /**
         * Used to get the error response of the booking list api
         * @param state: State is the object data of credential passed
         */
        failureBookingSummary: (state) => {
            state.isbookingSummaryLoading = false;
            state.bookingSummary = null;
        },

        /**
         * Used to start the request of create booking api call
         * @param state: State is the object data of credential passed
         */
        requestCreateBooking: (state) => {
            state.isCreateBookingLoading = true;
            state.createBookingData = null;
        },
        /**
         * Used to get the actual success response of the create booking api
         * @param state: State is the object data of credential passed
         */
        successCreateBooking: (state, action: PayloadAction<any>) => {
            state.createBookingData = action.payload;
            state.isCreateBookingLoading = false;
        },
        /**
         * Used to get the error response of the create booking api
         * @param state: State is the object data of credential passed
         */
        failureCreateBooking: (state) => {
            state.isCreateBookingLoading = false;
            state.createBookingData = null;
        },

        /**
         * Used to start the request of view booking api call
         * @param state: State is the object data of credential passed
         */
        requestViewBooking: (state) => {
            state.isViewBookingLoading = true;
            state.viewBookingData = null;
        },
        /**
         * Used to get the actual success response of the view booking api
         * @param state: State is the object data of credential passed
         */
        successViewBooking: (state, action: PayloadAction<any>) => {
            state.viewBookingData = action.payload;
            state.isViewBookingLoading = false;
        },
        /**
         * Used to get the error response of the view booking api
         * @param state: State is the object data of credential passed
         */
        failureViewBooking: (state) => {
            state.isViewBookingLoading = false;
            state.viewBookingData = null;
        },

        /**
         * Used to start the request of payment details list api call
         * @param state: State is the object data of credential passed
         */
        requestPaymentDetailsList: (state) => {
            state.isPaymentLoading = true;
            state.paymentDetailsList = null;
        },
        /**
         * Used to get the actual success response of the payment details list api
         * @param state: State is the object data of credential passed
         */
        successPaymentDetailsList: (state, action: PayloadAction<any>) => {
            state.paymentDetailsList = action.payload;
            state.isPaymentLoading = false;
        },
        /**
         * Used to get the error response of the payment details list api
         * @param state: State is the object data of credential passed
         */
        failurePaymentDetailsList: (state) => {
            state.isPaymentLoading = false;
            state.paymentDetailsList = null;
        },

        /**
         * Used to start the request of payment details add/edit api call
         * @param state: State is the object data of credential passed
         */
        requestPaymentDetailsAddEdit: (state) => {
            state.isPaymentAddEditLoading = true;
            state.paymentDetailsAddEdit = null;
        },
        /**
         * Used to get the actual success response of the payment details add/edit api
         * @param state: State is the object data of credential passed
         */
        successPaymentDetailsAddEdit: (state, action: PayloadAction<any>) => {
            state.paymentDetailsAddEdit = action.payload;
            state.isPaymentAddEditLoading = false;
        },
        /**
         * Used to get the error response of the payment details add/edit api
         * @param state: State is the object data of credential passed
         */
        failurePaymentDetailsAddEdit: (state) => {
            state.isPaymentAddEditLoading = false;
            state.paymentDetailsAddEdit = null;
        },

        /**
         * Resets the booking summary state.
         * Sets `isbookingSummaryLoading` to false and clears `bookingSummary`.
         * @param state - The current state object of the booking slice.
         */
        resetBookingSummary: (state) => {
            state.bookingSummary = null;
            state.isbookingSummaryLoading = false;
        },

        /**
         * Resets the view property availability state.
         * Sets `isLoading` to false and clears `viewPropertyAvailability`.
         * @param state - The current state object of the booking slice.
         */
        resetViewPropertyAvailability: (state) => {
            state.viewPropertyAvailability = null;
            state.isLoading = false;
        },

        /**
         * Used to start the request of cancel booking api call
         * @param state: State is the object data of credential passed
         */
        requestCancelBooking: (state) => {
            state.isCancelBookingLoading = true;
        },
        /**
         * Used to get the actual success response of the cancel booking api
         * @param state: State is the object data of credential passed
         */
        successCancelBooking: (state, action: PayloadAction<any>) => {
            state.isCancelBookingLoading = false;
        },
        /**
         * Used to get the error response of the booking list api
         * @param state: State is the object data of credential passed
         */
        failureCancelBooking: (state) => {
            state.isCancelBookingLoading = false;
        },

        /**
         * Sets `isRaiseDisputeLoading` to true, indicating that the raise dispute
         * API call is in progress.
         * @param state - The current state object of the booking slice.
         */
        requestRaiseDispute: (state) => {
            state.isRaiseDisputeLoading = true;
        },

        /**
         * Sets `isRaiseDisputeLoading` to false, indicating that the raise dispute
         * API call has completed successfully.
         * @param state - The current state object of the booking slice.
         * @param action - The action object containing the payload returned by the API call.
         */
        successRaiseDispute: (state, action: PayloadAction<any>) => {
            state.isRaiseDisputeLoading = false;
        },

        /**
         * Sets `isRaiseDisputeLoading` to false, indicating that the raise dispute
         * API call has failed.
         * @param state - The current state object of the booking slice.
         */
        failureRaiseDispute: (state) => {
            state.isRaiseDisputeLoading = false;
        },
    },
})

export const bookingActions = bookingSlice.actions
export default bookingSlice.reducer;