/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/consistent-type-imports */
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { gatewaysState } from "./type";
import { Gateways, GatewaysResponse } from "@/types/gateways";
import { addGateways, getGateways, getGatewaysById, getSociete } from "./thunk";

const initialState: gatewaysState = {
    status: "idle",
    error: null,
    gateways: [],
    gatewaysSelected: null,
    societe: null
};

const gatewaysSlice = createSlice({
  name: "gateways",
  initialState,
  reducers: {
    selectgateways: (state, action: PayloadAction<Gateways>) => {
      state.gatewaysSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


  /**
   * get societe
   */
    builder.addCase(getSociete.fulfilled, (state, action) => {
      const data = action.payload  
      console.log("data : ",data);
      
      state.societe = data;
      state.error = null;
      state.status = "succeeded";
    });

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

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



  /**
   * get all gateways
   */
    builder.addCase(getGateways.fulfilled, (state, action) => {
      const data = action.payload  

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

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

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

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

    builder.addCase(getGatewaysById.fulfilled, (state, action) => {
      const data = action.payload as Gateways;
      
      const idx = state.gateways.findIndex((n) => n.id === data.id);

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

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

    builder.addCase(getGatewaysById.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "An error occurred";
    });
  /**
   * add gateways
   */
    builder.addCase(addGateways.fulfilled, (state, action) => {
      
      const data = action.payload as any
      
      state.gateways.unshift(data);
      state.status = "succeeded";
      state.error = null;
    });

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

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


export const { selectgateways } = gatewaysSlice.actions
export default gatewaysSlice.reducer;


