import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import type { AppState, FilterValues } from "../utils/types/state_types";
import { Customer } from "../utils/types";
import { MatchProfile } from "../utils/types/state_types";

type MatchProfileFieldValue = string[] | number;

const initialFilterValues: FilterValues = {
  relationship_type: "",
  age_range: "",
  state: "",
  height_range: "",
  weight_range: "",
  religion: "",
  nationality: "",
  ethnicity: "",
};

const initialState: AppState = {
  isJoining: false,
  isMobileScreen: false,
  login: false,
  filterValues: initialFilterValues,
  search: "",
  sidebarOpen: false,
  openActionMenuId: null,
  viewCustomer: {
    isOpen: false,
    customerData: null,
  },
  profileSetup: {
    personalInfo: {
      mobileNumber: 0,
      fullName: "",
      dob: "",
      gender: "",
    },
    bioDataInfo: {
      maritalStatus: "",
      heightRange: "",
      weightRange: "",
      ageRange: "",
      ethnicity: "",
      genotype: "",
      occupation: "",
      religion: {
        value: "",
        subValue: "",
      },
    },
    contactInfo: {
      physicalAddress: "",
      streetName: "",
      lga: "",
      landmark: "",
      postalCode: "",
      state: "",
      country: "",
    },
    "hobbies&interests": {
      hobbies: [],
      interests: [],
      languages: [],
    },
  },
  matchProfiles: [
    {
      id: Date.now(), // or a fixed id like 1 if needed
      relationshipType: [],
      gender: [],
      ageRange: [],
      heightRange: [],
      weightRange: [],
      maritalStatus: [],
      ethnicity: [],
      genotype: [],
      state: [],
      religion: [],
      languages: [],
    },
  ],
  adminSettings: {
    relationship: {
      name: "",
      types: [],
    },
    stats: {
      total_sales: 0,
      total_users_count: 0,
      new_users_count: 0,
      deleted_accounts_count: 0,
      verified_users_count: 0,
    },
  },
};

const appSlice = createSlice({
  name: "app",
  initialState,
  reducers: {
    setJoinState: (state, action: PayloadAction<boolean>) => {
      state.isJoining = action.payload;
    },
    setIsMobileScreen: (state, action: PayloadAction<boolean>) => {
      state.isMobileScreen = action.payload;
    },
    setIsLogin: (state) => {
      state.login = !state.login;
    },
    updateFilterValue: (
      state,
      action: PayloadAction<{ field: keyof FilterValues; value: string }>
    ) => {
      const { field, value } = action.payload;
      state.filterValues[field] = value;
    },
    resetFilterValues: (state) => {
      state.filterValues = initialFilterValues;
    },
    updateSearch: (state, action: PayloadAction<string>) => {
      state.search = action.payload;
    },
    toggleSidebar: (state) => {
      state.sidebarOpen = !state.sidebarOpen;
    },
    setSidebarOpen: (state, action: PayloadAction<boolean>) => {
      state.sidebarOpen = action.payload;
    },
    setOpenActionMenu: (state, action: PayloadAction<number | null>) => {
      state.openActionMenuId = action.payload;
    },
    closeActionMenu: (state) => {
      state.openActionMenuId = null;
    },
    setViewCustomer: (state, action: PayloadAction<Customer | null>) => {
      state.viewCustomer = {
        isOpen: true,
        customerData: action.payload,
      };
    },
    closeViewCustomer: (state) => {
      state.viewCustomer = {
        isOpen: false,
        customerData: null,
      };
    },
    updatePersonalInfo: (
      state,
      action: PayloadAction<Partial<AppState["profileSetup"]["personalInfo"]>>
    ) => {
      state.profileSetup.personalInfo = {
        ...state.profileSetup.personalInfo,
        ...action.payload,
      };
    },
    updateBioDataInfo: (
      state,
      action: PayloadAction<Partial<AppState["profileSetup"]["bioDataInfo"]>>
    ) => {
      state.profileSetup.bioDataInfo = {
        ...state.profileSetup.bioDataInfo,
        ...action.payload,
      };
    },
    updateContactInfo: (
      state,
      action: PayloadAction<Partial<AppState["profileSetup"]["contactInfo"]>>
    ) => {
      state.profileSetup.contactInfo = {
        ...state.profileSetup.contactInfo,
        ...action.payload,
      };
    },
    updateHobbiesAndInterests: (
      state,
      action: PayloadAction<
        Partial<AppState["profileSetup"]["hobbies&interests"]>
      >
    ) => {
      state.profileSetup["hobbies&interests"] = {
        ...state.profileSetup["hobbies&interests"],
        ...action.payload,
      };
    },
    updateMatchProfileAtIndex: (
      state,
      action: PayloadAction<{
        index: number;
        field: keyof MatchProfile;
        value: MatchProfileFieldValue;
      }>
    ) => {
      const { index, field, value } = action.payload;
      if (state.matchProfiles[index]) {
        (state.matchProfiles[index][field] as typeof value) = value;
      }
    },
    addMatchProfile: (state) => {
      state.matchProfiles.push({
        id: Date.now(),
        relationshipType: [],
        gender: [],
        ageRange: [],
        heightRange: [],
        weightRange: [],
        maritalStatus: [],
        ethnicity: [],
        genotype: [],
        state: [],
        religion: [],
        languages: [],
      });
    },
    removeLastMatchProfile: (state) => {
      if (state.matchProfiles.length > 1) {
        state.matchProfiles.pop();
      }
    },
    setRelationShipName: (
      state,
      action: PayloadAction<Partial<AppState["adminSettings"]["relationship"]>>
    ) => {
      state.adminSettings.relationship = {
        ...state.adminSettings.relationship,
        name: action.payload.name ?? "",
      };
    },
    setRelationShipType: (
      state,
      action: PayloadAction<Partial<AppState["adminSettings"]["relationship"]>>
    ) => {
      state.adminSettings.relationship = {
        ...state.adminSettings.relationship,
        types: action.payload.types ?? [],
      };
    },

    setAdminStats: (
      state,
      action: PayloadAction<Partial<AppState["adminSettings"]["stats"]>>
    ) => {
      state.adminSettings.stats = {
        ...state.adminSettings.stats,
        total_sales: action.payload.total_sales ?? 0,
        total_users_count: action.payload.total_users_count ?? 0,
        deleted_accounts_count: action.payload.deleted_accounts_count ?? 0,
        new_users_count: action.payload.new_users_count ?? 0,
        verified_users_count: action.payload.verified_users_count ?? 0,
      };
    },
  },
});

export const {
  setJoinState,
  setIsMobileScreen,
  setIsLogin,
  updateFilterValue,
  resetFilterValues,
  updateSearch,
  toggleSidebar,
  setSidebarOpen,
  setOpenActionMenu,
  setViewCustomer,
  closeActionMenu,
  closeViewCustomer,
  updateBioDataInfo,
  updateContactInfo,
  updateHobbiesAndInterests,
  updatePersonalInfo,
  updateMatchProfileAtIndex,
  addMatchProfile,
  removeLastMatchProfile,
  setRelationShipName,
  setRelationShipType,
  setAdminStats,
} = appSlice.actions;
export default appSlice.reducer;
