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

import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { factureState } from "./type";
import { Facture } from "@/types/factureType";
import { changerStatutFacture, createFacture, getFacture, getFactures, selectFactureAsync, updateFacture, validerFacture } from "./thunk";
import { StatusResponse } from "@/types/sageTypes";

const initialState: factureState = {
    status: "idle",
    error: null,
    factures: [],
    factureSelected: null,
};

const factureSlice = createSlice({
  name: "facture",
  initialState,
  reducers: {
    selectfacture: (state, action: PayloadAction<Facture>) => {
      state.factureSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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



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

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

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



  /**
   * verified bg
   */
    builder.addCase(selectFactureAsync.fulfilled, (state, action) => {
      state.factureSelected = {
        ...action.payload
      };
      const idx = state.factures.findIndex(d => d.numero === action.payload.numero);
      if (idx !== -1) {
        state.factures[idx] = state.factureSelected;
      } else {
        state.factures.push(state.factureSelected);
      }
    });


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

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

      const idx = state.factures.findIndex((n) => n.numero === data.document_id);
      
      if (idx !== -1) {
        const oldItem = state.factures[idx];

        state.factures[idx] = {
          ...oldItem,
          statut: data.statut_nouveau
        };
      }

      if (state.factureSelected?.numero === data.document_id) {
        const oldItem = state.factureSelected;

        state.factureSelected= {
          ...oldItem,
          statut: data.statut_nouveau

        };
      }

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

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


export const { selectfacture } = factureSlice.actions
export default factureSlice.reducer;


