import { configureStore } from '@reduxjs/toolkit';
import PaymentSlice, { fetchInvoices, fetchInvoice, fetchTransactions, fetchTransaction } from '../PaymentSlice';

jest.mock('@/services/api/api', () => ({
  apiGet: jest.fn(),
  apiPost: jest.fn(),
  apiPut: jest.fn(),
  apiDelete: jest.fn(),
}));

const createStore = () => configureStore({ reducer: { payment: PaymentSlice.reducer } });

describe('PaymentSlice', () => {
  it('has correct initial state', () => {
    const store = createStore();
    const state = store.getState().payment;
    expect(state.loadingTransactions).toBe(false);
    expect(state.loadingInvoices).toBe(false);
    expect(state.error).toBeNull();
    expect(state.invoice).toBeNull();
    expect(state.invoices).toEqual([]);
    expect(state.transaction).toBeNull();
    expect(state.transactions).toEqual([]);
  });

  describe('fetchInvoices', () => {
    it('sets loadingInvoices on pending', () => {
      const store = createStore();
      store.dispatch({ type: fetchInvoices.pending.type });
      expect(store.getState().payment.loadingInvoices).toBe(true);
    });

    it('populates invoices on fulfilled', () => {
      const store = createStore();
      const invoices = [{ id: 'i1', amount: 100 }];
      store.dispatch({ type: fetchInvoices.fulfilled.type, payload: invoices });
      expect(store.getState().payment.invoices).toEqual(invoices);
      expect(store.getState().payment.loadingInvoices).toBe(false);
    });

    it('sets error on rejected', () => {
      const store = createStore();
      store.dispatch({ type: fetchInvoices.rejected.type });
      expect(store.getState().payment.error).toBe('Failed to fetch invoices');
    });
  });

  describe('fetchInvoice', () => {
    it('sets invoice on fulfilled', () => {
      const store = createStore();
      const invoice = { id: 'i1', amount: 200 };
      store.dispatch({ type: fetchInvoice.fulfilled.type, payload: invoice });
      expect(store.getState().payment.invoice).toEqual(invoice);
    });
  });

  describe('fetchTransactions', () => {
    it('sets loadingTransactions on pending', () => {
      const store = createStore();
      store.dispatch({ type: fetchTransactions.pending.type });
      expect(store.getState().payment.loadingTransactions).toBe(true);
    });

    it('populates transactions on fulfilled', () => {
      const store = createStore();
      const transactions = [{ id: 't1', amount: 50 }];
      store.dispatch({ type: fetchTransactions.fulfilled.type, payload: transactions });
      expect(store.getState().payment.transactions).toEqual(transactions);
      expect(store.getState().payment.loadingTransactions).toBe(false);
    });
  });

  describe('fetchTransaction', () => {
    it('sets transaction on fulfilled', () => {
      const store = createStore();
      const transaction = { id: 't1', amount: 50 };
      store.dispatch({ type: fetchTransaction.fulfilled.type, payload: transaction });
      expect(store.getState().payment.transaction).toEqual(transaction);
    });
  });
});
