// import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
// import axios from "../../app/api";

// // ✅ Async thunk to fetch dashboard stats
// export const fetchDashboard = createAsyncThunk(
//   "dashboard/fetchDashboard",
//   async (_, { rejectWithValue }) => {
//     try {
//       const res = await axios.get("/admin/dashboard");
//       return res.data.data;
//     } catch (error) {
//       return rejectWithValue(error.response?.data || error.message);
//     }
//   }
// );

// const dashboardSlice = createSlice({
//   name: "dashboard",
//   initialState: {
//     stats: null,
//     loading: false,
//     error: null,
//   },
//   reducers: {},
//   extraReducers: (builder) => {
//     builder
//       .addCase(fetchDashboard.pending, (state) => {
//         state.loading = true;
//         state.error = null;
//       })
//       .addCase(fetchDashboard.fulfilled, (state, action) => {
//         state.loading = false;
//         state.stats = action.payload;
//       })
//       .addCase(fetchDashboard.rejected, (state, action) => {
//         state.loading = false;
//         state.error = action.payload;
//       });
//   },
// });

// export default dashboardSlice.reducer;

import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import axios from "../../app/api";

/* =====================================================
   1️⃣ Fetch Main Dashboard Data
   Endpoint: GET /admin/dashboard
===================================================== */
export const fetchDashboard = createAsyncThunk(
  "dashboard/fetchDashboard",
  async (_, { rejectWithValue }) => {
    try {
      const res = await axios.get("/admin/dashboard");
      return res.data.data;
    } catch (error) {
      return rejectWithValue(error.response?.data?.message || error.message);
    }
  },
);

/* =====================================================
   2️⃣ Fetch Coupon Statistics
   Endpoint: GET /admin/stats
===================================================== */
export const fetchCouponStats = createAsyncThunk(
  "dashboard/fetchCouponStats",
  async (_, { rejectWithValue }) => {
    try {
      const res = await axios.get("/admin/stats");
      return res.data.data;
    } catch (error) {
      return rejectWithValue(error.response?.data?.message || error.message);
    }
  },
);

/* =====================================================
   Dashboard Slice
===================================================== */
const dashboardSlice = createSlice({
  name: "dashboard",
  initialState: {
    stats: null, // data from /admin/dashboard
    couponStats: null, // data from /admin/stats
    loading: false,
    error: null,
  },
  reducers: {},
  extraReducers: (builder) => {
    builder

      /* ===== DASHBOARD DATA ===== */
      .addCase(fetchDashboard.pending, (state) => {
        state.loading = true;
        state.error = null;
      })
      .addCase(fetchDashboard.fulfilled, (state, action) => {
        state.loading = false;
        state.stats = action.payload;
      })
      .addCase(fetchDashboard.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload;
      })

      /* ===== COUPON STATS ===== */
      .addCase(fetchCouponStats.pending, (state) => {
        state.loading = true;
      })
      .addCase(fetchCouponStats.fulfilled, (state, action) => {
        state.loading = false;
        state.couponStats = action.payload;
      })
      .addCase(fetchCouponStats.rejected, (state, action) => {
        state.loading = false;
        state.error = action.payload;
      });
  },
});

export default dashboardSlice.reducer;
