import { fetchApi } from './client';
import type { PaymentOptionsConfig } from './types';

export const paymentOptionsApi = {
  // Global payment options
  getGlobal: () =>
    fetchApi<{ paymentOptions: PaymentOptionsConfig }>('/api/payment-options'),

  updateGlobal: (data: Partial<PaymentOptionsConfig>) =>
    fetchApi<{ paymentOptions: PaymentOptionsConfig; message: string }>('/api/payment-options', {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  // Event-specific options (merged with global). Pass ticketId after booking to
  // retrieve bank/TPago credentials gated behind the booking capability token.
  getForEvent: (eventId: string, ticketId?: string) =>
    fetchApi<{ paymentOptions: PaymentOptionsConfig; hasOverrides: boolean }>(
      `/api/payment-options/event/${eventId}${ticketId ? `?ticketId=${encodeURIComponent(ticketId)}` : ''}`
    ),

  // Event overrides (admin only)
  getEventOverrides: (eventId: string) =>
    fetchApi<{ overrides: Partial<PaymentOptionsConfig> | null }>(
      `/api/payment-options/event/${eventId}/overrides`
    ),

  updateEventOverrides: (eventId: string, data: Partial<PaymentOptionsConfig>) =>
    fetchApi<{ overrides: Partial<PaymentOptionsConfig>; message: string }>(
      `/api/payment-options/event/${eventId}/overrides`,
      {
        method: 'PUT',
        body: JSON.stringify(data),
      }
    ),

  deleteEventOverrides: (eventId: string) =>
    fetchApi<{ message: string }>(`/api/payment-options/event/${eventId}/overrides`, {
      method: 'DELETE',
    }),
};
