/**
 * @author: Multiqos
 * File: propertySlice.ts
 * Description:
 * - This file defines the Redux slice for managing property-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 property scenarios such as property list, etc.
*/
import { ListFilters } from '@/redux/types/propertyTypes';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';

interface PropertyState {
    propertyList: any;
    isLoading: boolean;
    allProperties: any[];
    propertyMeta: null | any;
    sortKey: null | any;
    locationList: any;
    isLocationLoading: boolean;
    searchData: any;
    isPageLoading: boolean;
    currentPage: number;
    filterListLoading: boolean,
    filtersList: ListFilters | null;
    filterListMeta: null | any
}

const initialState: PropertyState = {
    propertyList: null,
    isLoading: true,
    allProperties: [],
    propertyMeta: null,
    sortKey: { slug: "newest-listings", title: "Newest Listings" },
    locationList: null,
    isLocationLoading: false,
    searchData: null,
    isPageLoading: true,
    currentPage: 1,
    filterListLoading: false,
    filtersList: null,
    filterListMeta: null,
}

const propertySlice = createSlice({
    name: 'property',
    initialState,
    reducers: {
        /**
         * Used to start the request of property list api call
         * @param state: State is the object data of credential passed
         */
        requestPropertyList: (state) => {
            state.isLoading = true;
        },
        /**
         * Used to get the actual success response of the property list api
         * @param state: State is the object data of credential passed
         */
        successPropertyList: (
            state,
            action: PayloadAction<any>
        ) => {
            const { data, meta, page } = action.payload;

            // Replace propertyList (original use)
            state.propertyList = { data, meta };
            // Only set sortKey if it's not already set
            // if (state.sortKey == null && meta?.filters?.propertySortBy?.[0]) {
            //     state.sortKey = meta.filters.propertySortBy[0];
            // }

            // Append or reset allProperties based on page
            if (page === 1) {
                state.allProperties = data;
            } else {
                state.allProperties = [...state.allProperties, ...data];
            }

            state.propertyMeta = meta;
            state.isLoading = false;
            state.isPageLoading = false;
        },
        /**
         * Used to get the error response of the property list api
         * @param state: State is the object data of credential passed
         */
        failurePropertyList: (state) => {
            state.isLoading = false;
            state.propertyList = null;
        },
        /**
        * Used to start the request of property list api call
        * @param state: State is the object data of credential passed
        */
        requestFilterPropertyList: (state) => {
            state.filterListLoading = true;
        },
        /**
         * Used to get the actual success response of the property list api
         * @param state: State is the object data of credential passed
         */
        successFilterPropertyList: (
            state,
            action: PayloadAction<any>
        ) => {
            const { meta } = action.payload;

            state.filterListMeta = meta;
            state.filterListLoading = false;
        },
        /**
         * Used to get the error response of the property list api
         * @param state: State is the object data of credential passed
         */
        failureFilterPropertyList: (state) => {
            state.filterListLoading = false;
        },
        /**
         * Used to get the SortKey dropdown response when changed selection of dropdown
         * @param state 
         * @param action 
         */
        setSortKey: (state, action: PayloadAction<any>) => {
            state.sortKey = action.payload
        },

        /**
         * Used to start the request of location list api call
         * @param state: State is the object data of credential passed
         */
        requestLocationList: (state) => {
            state.isLocationLoading = true;
        },
        /**
         * Used to get the actual success response of the location list api
         * @param state: State is the object data of credential passed
         */
        successLocationList: (state, action: PayloadAction<any>) => {
            state.locationList = action.payload;
            state.isLocationLoading = false;
        },
        /**
         * Used to get the error response of the location list api
         * @param state: State is the object data of credential passed
         */
        failureLocationList: (state) => {
            state.isLocationLoading = false;
            state.locationList = null;
        },

        /**
         * Used to get the actual success response of the search data
         * @param state: State is the object data of credential passed
         */
        successSearchData: (state, action: PayloadAction<any>) => {
            state.searchData = action.payload;
        },

        /**
         * Used to get the actual success response of the current page
         * @param state: State is the object data of credential passed
         */
        successCurrentPage: (state, action: PayloadAction<any>) => {
            state.currentPage = action.payload;
        },

        /**
         * Used to start the request of filters list api call
         * @param state: State is the object data of initialState
         */
        requestFiltersList: (state) => {
            state.filterListLoading = true;
        },
        /**
         * Used to get the actual success response of the filters list api
         * @param state: State is the object data of initialState
         */
        successFiltersList: (state, action: PayloadAction<any>) => {
            state.filtersList = action.payload;
            state.filterListLoading = false;
        },
        /**
         * Used to get the error response of the filters list api
         * @param state: State is the object data of initialState
         */
        failureFiltersList: (state) => {
            state.filterListLoading = false;
            state.filtersList = null;
        },
        /**
         * Used to reset property list
         */
        resetPropertyListState: (state) => {
            state.allProperties = [];
            state.propertyList = null;
            state.isPageLoading = true;
        },
    },
})

export const propertyActions = propertySlice.actions
export default propertySlice.reducer;







