// store/emiSlice.ts
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
import { MyPlan } from "@/app/(tabs)/payments";

// Define the payment order interface based on your API response
interface PaymentOrder {
  amount: number;
  expiry_date: string;
  order_id: string;
  ppayment_link: string;
  qr_code_url: string;
  status: string;
  transaction_id: string;
  upi_handle: string;
}

interface EmiState {
  due_amount: number | null;
  myPlan: MyPlan | null;
  paymentOrder: PaymentOrder | null;
}

const initialState: EmiState = {
  due_amount: null,
  myPlan: null,
  paymentOrder: null,
};

const emiSlice = createSlice({
  name: "emi",
  initialState,
  reducers: {
    setDueAmount(state, action: PayloadAction<number | null>) {
      state.due_amount = action.payload;
    },
    setMyPlan(state, action: PayloadAction<MyPlan | null>) {
      state.myPlan = action.payload;
    },
    setPaymentOrder(state, action: PayloadAction<PaymentOrder | null>) {
      state.paymentOrder = action.payload;
    },
    clearPaymentOrder(state) {
      state.paymentOrder = null;
    },
    updatePaymentStatus(state, action: PayloadAction<string>) {
      if (state.paymentOrder) {
        state.paymentOrder.status = action.payload;
      }
    },
  },
});

export const {
  setDueAmount,
  setMyPlan,
  setPaymentOrder,
  clearPaymentOrder,
  updatePaymentStatus,
} = emiSlice.actions;

export default emiSlice.reducer;
