/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/consistent-type-imports */
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { BLState } from "./type";
import { BL } from "@/types/BL_Types";
import { blToFacture, changerStatutBL, createBL, getAllBL, getBL, selectBLAsync, updateBL } from "./thunk";
import { StatusResponse, TransformationResponse } from "@/types/sageTypes";

const initialState: BLState = {
    status: "idle",
    error: null,
    BLs: [],
    BLSelected: null,
};

const BLSlice = createSlice({
  name: "BL",
  initialState,
  reducers: {
    selectBL: (state, action: PayloadAction<BL>) => {
      state.BLSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


  /**
   * get all BLs
   */
    builder.addCase(getAllBL.fulfilled, (state, action) => {
      const data = action.payload;
      
      state.BLs = data;
      state.error = null;
      state.status = "succeeded";
    });

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

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

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

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

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

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

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

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


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

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

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

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

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

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


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

    /**
     * BL to facture
     */
    builder.addCase(blToFacture.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(blToFacture.fulfilled, (state, action) => {
      const data = action.payload as TransformationResponse;
      const idx = state.BLs.findIndex((n) => n.numero === data.document_source);
      if (idx !== -1) {
        state.BLs.splice(idx, 1)
      }

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

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

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

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

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

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

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

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

        };
      }

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

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



  },
});


export const { selectBL } = BLSlice.actions
export default BLSlice.reducer;


