import { createSlice } from "@reduxjs/toolkit";

const localVariablesSlice = createSlice({
  name: "localVariables",
  initialState: {
    selectedMaster: "Application Role",
    //  Attendances
    attendance: {
      regularizationCount: 0,
      defaultDateRange: { startDate: null, endDate: null },
      SelectedOrg: "",
    },

    // PopUp
    popups: {},

    // Modal for all simple pass Name

    modals: {
      auth: { isOpen: false },
      organization: { isOpen: false },
    },
    projectId: null,
    reload: false,

    OrganizationModal: {
      isOpen: false,
      orgData: null,
      prevStep: null,
      startStep: 1,
      flowType: "default",
    },

    AuthModal: {
      isOpen: false,
    },

    selfTenant: {
      tenantEnquireId: null,
      planId: null,
      details: null,
      frequency: null,
      paymentDetailId: null,
    },
  },
  reducers: {
    changeMaster: (state, action) => {
      state.selectedMaster = action.payload;
    },

    // ─── ATTENDANCE ─────────────────────────
    updateRegularizationCount: (state, action) => {
      state.attendance.regularizationCount = action.payload;
    },
    setDefaultDateRange: (state, action) => {
      state.attendance.defaultDateRange = action.payload;
    },
    setOrganization: (state, action) => {
      state.attendance.SelectedOrg = action.payload;
    },

    setProjectId: (state, action) => {
      localStorage.setItem("project", null);
      state.projectId = action.payload;
      localStorage.setItem("project", state.projectId || null);
    },
    refreshData: (state, action) => {
      state.reload = action.payload;
    },
    openOrgModal: (state, action) => {
      state.OrganizationModal.isOpen = true;
      state.OrganizationModal.orgData = action.payload?.orgData || null;

      if (state.OrganizationModal.startStep) {
        state.OrganizationModal.prevStep = state.OrganizationModal.startStep;
      }

      state.OrganizationModal.startStep = action.payload?.startStep || 1;
      state.OrganizationModal.flowType = action.payload?.flowType || "default";
    },
    closeOrgModal: (state) => {
      state.OrganizationModal.isOpen = false;
      state.OrganizationModal.orgData = null;
      state.OrganizationModal.startStep = 1;
      state.OrganizationModal.prevStep = null;
    },
    toggleOrgModal: (state) => {
      state.OrganizationModal.isOpen = !state.OrganizationModal.isOpen;
    },

    openAuthModal: (state, action) => {
      state.AuthModal.isOpen = true;
    },
    closeAuthModal: (state, action) => {
      state.AuthModal.isOpen = false;
    },

    openModal: (state, action) => {
      const { modalType, data } = action.payload;

      state.modals[modalType] = {
        isOpen: true,
        data: data ?? {},
      };
    },

    closeModal: (state, action) => {
      const { modalType } = action.payload;
      state.modals[modalType] = {
        isOpen: false,
        data: null,
      };
    },

    toggleModal: (state, action) => {
      const { modalType } = action.payload;
      const modal = state.modals[modalType] || {};
      modal.isOpen = !modal.isOpen;
    },

    setSelfTenant: (state, action) => {
      state.selfTenant.tenantEnquireId =
        action.payload.tenantEnquireId ?? state.selfTenant.tenantEnquireId;
      state.selfTenant.planId =
        action.payload.planId ?? state.selfTenant.planId;
      state.selfTenant.details =
        action.payload.details ?? state.selfTenant.details;
      state.selfTenant.frequency =
        action.payload.frequency ?? state.selfTenant.frequency;
      state.selfTenant.paymentDetailId =
        action.payload.paymentDetailId ?? state.selfTenant.paymentDetailId;
    },

     openPopup: (state, action) => {
      const id = action.payload;
      state.popups[id] = true;
    },
    closePopup: (state, action) => {
      const id = action.payload;
      state.popups[id] = false;
    },
    togglePopup: (state, action) => {
      const id = action.payload;
      state.popups[id] = !state.popups[id];
    }
  },
});

export const {
  changeMaster,
  updateRegularizationCount,
  setProjectId,
  refreshData,
  setDefaultDateRange,
  openOrgModal,
  closeOrgModal,
  toggleOrgModal,
  openAuthModal,
  closeAuthModal,
  setOrganization,
  openModal,
  closeModal,
  toggleModal,
  setSelfTenant,openPopup, closePopup, togglePopup
} = localVariablesSlice.actions;
export default localVariablesSlice.reducer;
