/* eslint-disaAvoire import/order */
/* eslint-disaAvoire @typescript-eslint/consistent-type-imports */

import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { avoirState } from "./type";
import { Avoir } from "@/types/avoirType";
import { createAvoir, getAvoir, getAvoirs, selectAvoirAsync, updateAvoir } from "./thunk";

const initialState: avoirState = {
    status: "idle",
    error: null,
    avoirs: [],
    avoirSelected: null,
};

const avoirSlice = createSlice({
  name: "avoir",
  initialState,
  reducers: {
    selectavoir: (state, action: PayloadAction<Avoir>) => {
      state.avoirSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


  /**
   * get all avoir
   */
    builder.addCase(getAvoirs.fulfilled, (state, action) => {
      const data = action.payload;

      state.avoirs = data;
      state.error = null;
      state.status = "succeeded";
    });

    builder.addCase(getAvoirs.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(getAvoirs.rejected, (state, action) => {
      state.status = "failed";
      state.error = action.error.message || "An error occurred";
    });


    /**
     * get avoir by id
     */
    builder.addCase(getAvoir.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(getAvoir.fulfilled, (state, action) => {
      const data = action.payload as Avoir;

      const idx = state.avoirs.findIndex((n) => n.numero === data.numero);

      if (idx !== -1) {
        state.avoirs[idx] = data;
      } else {
        state.avoirs.push(data);
      }

      state.status = "succeeded";
      state.error = null;
    });

    builder.addCase(getAvoir.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "An error occurred";
    });


  /**
   * create Avoir
   */
    builder.addCase(createAvoir.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(createAvoir.fulfilled, (state) => {
      state.status = "succeeded";
      state.error = null;
    });

    builder.addCase(createAvoir.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "An error occurred";
    });

  /**
    * update Avoir
    */
    builder.addCase(updateAvoir.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(updateAvoir.fulfilled, (state) => {
      state.status = "succeeded";
      state.error = null;
    });

    builder.addCase(updateAvoir.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "An error occurred";
    });


  /**
   * verified Avoir
   */
    builder.addCase(selectAvoirAsync.fulfilled, (state, action) => {
      state.avoirSelected = {
        ...action.payload
      };
      const idx = state.avoirs.findIndex(d => d.numero === action.payload.numero);
      if (idx !== -1) {
        state.avoirs[idx] = state.avoirSelected;
      } else {
        state.avoirs.push(state.avoirSelected);
      }
    });

  },
});


export const { selectavoir } = avoirSlice.actions
export default avoirSlice.reducer;


