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

// Define the structure of a single religion object
interface Religion {
  religion: string;
  denomination: string;
}

// Define the structure of the form object
interface Form {
  id: number;
  relationshipType: string;
  ageRange: string[];
  genotype: string[];
  maritalStatus: string[];
  heightRange: string[];
  weightRange: string[];
  gender: string[];
  religions: Religion[];
  ethnicity: string[];
  languages: string[];
  states: string[];
  country: string[];
  hobbies: string[];
  interests: string[];
}

// Define the structure of the root state
interface FormState {
  matchup: Form[];
  newMatchup: Form[];
}

const createEmptyForm = (): Form => ({
  id: Date.now(),
  relationshipType: "",
  ageRange: [],
  genotype: [],
  maritalStatus: [],
  heightRange: [],
  weightRange: [],
  gender: [],
  religions: [],
  ethnicity: [],
  languages: [],
  states: [],
  country: [],
  hobbies: [],
  interests: [],
});

const initialState: FormState = {
  matchup: [createEmptyForm()],
  newMatchup: [createEmptyForm()],
};

export const matchupSlice = createSlice({
  name: "form",
  initialState,
  reducers: {
    // === MATCHUP ===
    addForm: (state) => {
      const lastForm = state.matchup[state.matchup.length - 1];
      const newForm = {
        ...createEmptyForm(),
        ...lastForm,
        relationshipType: "",
        id: Date.now(),
      };
      state.matchup.push(newForm);
    },

    removeForm: (state, action: PayloadAction<number>) => {
      if (state.matchup.length > 1) {
        const indexToRemove = state.matchup.findIndex(
          (form) => form.id === action.payload
        );
        if (indexToRemove !== -1) {
          state.matchup.splice(indexToRemove, 1);
        }
      }
    },

    updateForm: (
      state,
      action: PayloadAction<{ id: number; field: string; value: any }>
    ) => {
      const { id, field, value } = action.payload;
      const form: any = state.matchup.find((f) => f.id === id);
      console.log(value, "is value at update form");
      if (form) {
        if (field === "religions" && Array.isArray(value)) {
          form.religions = value.map((item: any) => ({
            religion: item.religion,
            denomination: item.denomination || "",
          }));
        } else {
          form[field] = value;
        }
      }
    },

    setMatchups: (state, action: PayloadAction<Form[]>) => {
      state.matchup = action.payload;
    },

    // === NEW MATCHUP ===
    addNewForm: (state) => {
      const lastForm = state.newMatchup[state.newMatchup.length - 1];
      const newForm = {
        ...createEmptyForm(),
        ...lastForm,
        relationshipType: "",
        id: Date.now(),
      };
      state.newMatchup.push(newForm);
    },

    removeNewForm: (state, action: PayloadAction<number>) => {
      if (state.newMatchup.length > 1) {
        const indexToRemove = state.newMatchup.findIndex(
          (form) => form.id === action.payload
        );
        if (indexToRemove !== -1) {
          state.newMatchup.splice(indexToRemove, 1);
        }
      }
    },

    updateNewForm: (
      state,
      action: PayloadAction<{ id: number; field: string; value: any }>
    ) => {
      const { id, field, value } = action.payload;
      const form: any = state.newMatchup.find((f) => f.id === id);
      if (form) {
        if (field === "religions") {
          const index = form.religions.findIndex(
            (r: any) => r.religion === value.religion
          );
          if (index !== -1) {
            form.religions[index] = {
              religion: value.religion,
              denomination: value.denomination,
            };
          } else {
            form.religions.push({
              religion: value.religion,
              denomination: value.denomination,
            });
          }
        } else {
          form[field] = value;
        }
      }
    },

    setNewMatchups: (state, action: PayloadAction<Form[]>) => {
      state.newMatchup = action.payload;
    },
    resetMatchupProfile: () => initialState,
  },
});

// Export actions
export const {
  addForm,
  removeForm,
  updateForm,
  setMatchups,
  addNewForm,
  removeNewForm,
  updateNewForm,
  setNewMatchups,
  resetMatchupProfile
} = matchupSlice.actions;

export default matchupSlice.reducer;
