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

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

    // 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,
    },
  },
  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 };
    },
    closeModal: (state, action) => {
      const { modalType } = action.payload;
      state.modals[modalType] = { ...state.modals[modalType], isOpen: false };
    },
    toggleModal: (state, action) => {
      const { modalType } = action.payload;
      state.modals[modalType].isOpen = !state.modals[modalType].isOpen;
    },
  },
});

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