/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/consistent-type-imports */
import { createSlice, type PayloadAction } from "@reduxjs/toolkit";
import { articleState } from "./type";
import { addStock, createArticle, getarticleById, getArticles, updateArticle } from "./thunk";
import { Article } from "@/types/articleType";

const initialState: articleState = {
    status: "idle",
    error: null,
    articles: [],
    articleSelected: null,
};

const articleSlice = createSlice({
  name: "article",
  initialState,
  reducers: {
    selectArticle: (state, action: PayloadAction<Article>) => {
      state.articleSelected = action.payload;
    }
  },
  extraReducers: (builder) => {


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

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

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

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

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

  builder.addCase(getarticleById.fulfilled, (state, action) => {
    const data = action.payload as Article;
    
    const idx = state.articles.findIndex((n) => n.reference === data.reference);

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

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

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


  /**
   * add article
   */
    builder.addCase(createArticle.fulfilled, (state, action) => {
      
      const data = action.payload as any
      
      state.articles.unshift(data);
      state.status = "succeeded";
      state.error = null;
    });

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

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


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

    builder.addCase(updateArticle.fulfilled, (state, action) => {
      const data = action.payload as Article
      const idx = state.articles.findIndex((p) => p.reference === data.reference);
      if (idx !== -1){
      state.articles[idx] = data
      }
      state.status = "succeeded";
      state.error = null;
    });

    builder.addCase(updateArticle.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "Failed to update";
    });



    /**
     * add stock
     */
    builder.addCase(addStock.pending, (state) => {
      state.status = "loading";
    });

    builder.addCase(addStock.fulfilled, (state, action) => {
      const { data, quantite } = action.payload;

      const idx = state.articles.findIndex(
        (p) => p.reference === data.article_ref
      );

      if (idx !== -1) {
        state.articles[idx].stock_reel += quantite;
        state.articles[idx].stock_disponible! += quantite;
      }

      if (state.articleSelected) {
        state.articleSelected.stock_reel += quantite;
        state.articleSelected.stock_disponible! += quantite;
      }

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


    builder.addCase(addStock.rejected, (state, action) => {
      state.status = "failed";
      state.error = (action.payload as string) || action.error.message || "Failed to update";
    });

  },
});


export const { selectArticle } = articleSlice.actions
export default articleSlice.reducer;


