export type GiftVoucherTier = {
  key: string;
  label: string;
  amount: number;
  currency: string;
  paddle_price_id?: string | null;
  can_checkout: boolean;
};

export type GiftVoucherCheckoutRequest = {
  tier_key: string;
  purchaser_email: string;
  recipient_email?: string;
  recipient_name?: string;
  message?: string;
  success_url?: string;
  return_url?: string;
};

export type GiftVoucherCheckoutResponse = {
  checkout_url: string | null;
  expires_at: string | null;
  id: string | null;
};

export type GiftVoucherLookupResponse = {
  code: string;
  amount: number;
  currency: string;
  expires_at: string | null;
  recipient_name?: string | null;
  recipient_email?: string | null;
  purchaser_email?: string | null;
  status: string;
  redeemed_at?: string | null;
  refunded_at?: string | null;
};

export async function fetchGiftVoucherTiers(): Promise<GiftVoucherTier[]> {
  const response = await fetch('/api/v1/marketing/gift-vouchers/tiers', {
    headers: {
      Accept: 'application/json',
    },
  });

  if (!response.ok) {
    throw new Error('Failed to load gift voucher tiers');
  }

  const payload = await response.json();

  return (payload?.data ?? []) as GiftVoucherTier[];
}

export async function createGiftVoucherCheckout(data: GiftVoucherCheckoutRequest): Promise<GiftVoucherCheckoutResponse> {
  const response = await fetch('/api/v1/marketing/gift-vouchers/checkout', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Accept: 'application/json',
    },
    body: JSON.stringify(data),
  });

  const payload = await response.json().catch(() => ({}));

  if (!response.ok) {
    const errors = (payload?.errors ?? {}) as Record<string, unknown>;
    const firstErrorEntry = Object.values(errors)[0];
    const firstError =
      Array.isArray(firstErrorEntry) && typeof firstErrorEntry[0] === 'string'
        ? firstErrorEntry[0]
        : null;
    const message = firstError || (typeof payload?.message === 'string' ? payload.message : null) || 'Unable to start checkout';
    throw new Error(message);
  }

  return payload as GiftVoucherCheckoutResponse;
}

export async function fetchGiftVoucherByCheckout(checkoutId?: string | null, transactionId?: string | null): Promise<GiftVoucherLookupResponse | null> {
  if (!checkoutId && !transactionId) {
    return null;
  }

  const params = new URLSearchParams();
  if (checkoutId) params.set('checkout_id', checkoutId);
  if (transactionId) params.set('transaction_id', transactionId);

  const response = await fetch(`/api/v1/marketing/gift-vouchers/lookup?${params.toString()}`, {
    headers: { Accept: 'application/json' },
  });

  if (!response.ok) {
    return null;
  }

  const payload = await response.json();

  return (payload?.data ?? null) as GiftVoucherLookupResponse | null;
}

export async function fetchGiftVoucherByCode(code: string): Promise<GiftVoucherLookupResponse | null> {
  const trimmed = code.trim();
  if (!trimmed) {
    return null;
  }

  const params = new URLSearchParams({ code: trimmed });
  const response = await fetch(`/api/v1/marketing/gift-vouchers/lookup?${params.toString()}`, {
    headers: { Accept: 'application/json' },
  });

  if (!response.ok) {
    return null;
  }

  const payload = await response.json();

  return (payload?.data ?? null) as GiftVoucherLookupResponse | null;
}
