import { createSlice, PayloadAction } from '@reduxjs/toolkit';

export interface Zone {
  id: string;
  name: string;
  description?: string;
  code: string;
  regionCount: number;
  zmCount: number;
  states: string[];
  zonalBusinessHead?: { id: string; name: string; email: string; phone?: string };
  zonalManagers: { id: string; name: string; email: string; phone: string; regions: string[] }[];
}

export interface Region {
  id: string;
  code: string;
  name: string;
  zoneId: string;
  zoneName: string;
  districts: { id: string; name: string, stateId?: string }[];
  states: string[];
  cities: string[];
  status: string;
  regionalOfficerCount: number;
  asmCount: number;
  regionalManager?: { id: string; fullName: string; email: string; phone?: string } | null;
}

export interface ASM {
  id: string;
  code: string;
  asmCode: string;
  employeeId: string;
  name: string;
  zoneId: string;
  regionId: string;
  zoneName: string;
  regionName: string;
  areasManaged: { id: string; name: string }[];
  districtNames: string[];
  stateNames: string[];
  email: string;
  phone: string;
  status: string;
}

export interface ZonalManagerMapping {
  id: string;
  name: string;
  code: string;
  email: string;
  phone: string;
  zoneId: string;
  zoneName: string;
  regionId: string;
  regionName: string;
  districts: { id: string; name: string }[];
  status: string;
  city?: string;
  openFrom?: string | Date | null;
  openTo?: string | Date | null;
}

export interface MasterState {
  zones: Zone[];
  regionalOffices: Region[];
  asms: ASM[];
  zonalManagerMappings: ZonalManagerMapping[];
  zonalManagers: any[];
  roles: any[];
  allStates: { id: string, name: string, zone?: { name: string } }[];
  allDistricts: {
    id: string,
    name: string,
    code?: string,
    stateId: string,
    regionId: string,
    zoneId: string,
    stateName: string,
    regionName: string,
    zoneName: string,
    asmName?: string,
    isOpportunity: boolean,
    city?: string,
    openFrom?: string | Date | null,
    openTo?: string | Date | null
  }[];
  allAreas: any[];
  availablePermissions: any[];
  emailTemplates: any[];
  slaConfigs: any[];
  users: any[];
  ddLeads: any[];
  areasPagination: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
  loading: boolean;
  isAreasLoading: boolean;
  error: string | null;
}

const initialState: MasterState = {
  zones: [],
  regionalOffices: [],
  asms: [],
  zonalManagerMappings: [],
  zonalManagers: [],
  roles: [],
  allStates: [],
  allDistricts: [],
  allAreas: [],
  availablePermissions: [],
  emailTemplates: [],
  slaConfigs: [],
  users: [],
  ddLeads: [],
  areasPagination: {
    total: 0,
    page: 1,
    limit: 10,
    totalPages: 0,
  },
  loading: false,
  isAreasLoading: false,
  error: null,
};

const masterSlice = createSlice({
  name: 'master',
  initialState,
  reducers: {
    setMasterData: (state, action: PayloadAction<Partial<MasterState>>) => {
      return { ...state, ...action.payload };
    },
    setAreasData: (state, action: PayloadAction<{ data: any[], pagination: any }>) => {
      state.allAreas = action.payload.data;
      state.areasPagination = action.payload.pagination;
    },
    setAreasLoading: (state, action: PayloadAction<boolean>) => {
      state.isAreasLoading = action.payload;
    },
    setZones: (state, action: PayloadAction<Zone[]>) => {
      state.zones = action.payload;
    },
    setRegionalOffices: (state, action: PayloadAction<Region[]>) => {
      state.regionalOffices = action.payload;
    },
    setAsms: (state, action: PayloadAction<ASM[]>) => {
      state.asms = action.payload;
    },
    setZonalManagerMappings: (state, action: PayloadAction<ZonalManagerMapping[]>) => {
      state.zonalManagerMappings = action.payload;
    },
    setUsers: (state, action: PayloadAction<any[]>) => {
      state.users = action.payload;
    },
    setLoading: (state, action: PayloadAction<boolean>) => {
      state.loading = action.payload;
    },
    setError: (state, action: PayloadAction<string | null>) => {
      state.error = action.payload;
    },
    setDdLeads: (state, action: PayloadAction<any[]>) => {
      state.ddLeads = action.payload;
    },
  },
});

export const {
  setMasterData, setZones, setRegionalOffices, setAsms,
  setZonalManagerMappings, setUsers, setLoading, setError,
  setAreasData, setAreasLoading, setDdLeads
} = masterSlice.actions;

export default masterSlice.reducer;
