import { create } from 'zustand';

interface PaymentState {
  paymentIntentId: string | null;
  paymentStatus: 'PENDING' | 'PROCESSING' | 'REQUIRES_ACTION' | 'SUCCEEDED' | 'FAILED' | null;
  // The currency actually confirmed for the selected payment option (from
  // /payments/booking-amount), not just a default — kept in sync by the payment page.
  selectedCurrency: 'ETB' | 'DJF' | 'USD';
  // The exact minor-unit amount confirmed for that currency/payment option. Downstream
  // screens (e.g. the voucher) should use this instead of recomputing a default ETB fare.
  paidAmountMinor: number | null;

  setPaymentIntent: (id: string) => void;
  updateStatus: (status: 'PENDING' | 'PROCESSING' | 'REQUIRES_ACTION' | 'SUCCEEDED' | 'FAILED') => void;
  setCurrency: (currency: 'ETB' | 'DJF' | 'USD') => void;
  setPaidAmount: (amountMinor: number) => void;
  clearPayment: () => void;
}

export const usePaymentStore = create<PaymentState>((set) => ({
  paymentIntentId: null,
  paymentStatus: null,
  selectedCurrency: 'ETB',
  paidAmountMinor: null,

  setPaymentIntent: (id) => set({ paymentIntentId: id }),
  updateStatus: (status) => set({ paymentStatus: status }),
  setCurrency: (currency) => set({ selectedCurrency: currency }),
  setPaidAmount: (amountMinor) => set({ paidAmountMinor: amountMinor }),
  clearPayment: () => set({
    paymentIntentId: null,
    paymentStatus: null,
    selectedCurrency: 'ETB',
    paidAmountMinor: null,
  }),
}));
