/**
 * Billing checkout tests — Zod schema validation and input handling.
 */

import { describe, it, expect } from 'vitest';
import { BillingCheckoutSchema } from './types.js';

describe('BillingCheckoutSchema', () => {
  it('validates with plan', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      plan: 'pro',
      successUrl: 'https://example.com/success',
      cancelUrl: 'https://example.com/cancel',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.plan).toBe('pro');
      expect(result.data.collectTax).toBe(false);
    }
  });

  it('validates with explicit priceId', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      priceId: 'price_abc123',
      successUrl: 'https://example.com/success',
      cancelUrl: 'https://example.com/cancel',
    });
    expect(result.success).toBe(true);
  });

  it('rejects when neither plan nor priceId provided', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      successUrl: 'https://example.com/success',
      cancelUrl: 'https://example.com/cancel',
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid plan', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      plan: 'platinum',
      successUrl: 'https://example.com/success',
      cancelUrl: 'https://example.com/cancel',
    });
    expect(result.success).toBe(false);
  });

  it('accepts optional fields', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      plan: 'enterprise',
      successUrl: 'https://example.com/success',
      cancelUrl: 'https://example.com/cancel',
      trialDays: 14,
      promoCode: 'LAUNCH50',
      collectTax: true,
      customerEmail: 'user@example.com',
      metadata: { campaign: 'onboarding' },
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.trialDays).toBe(14);
      expect(result.data.collectTax).toBe(true);
      expect(result.data.customerEmail).toBe('user@example.com');
      expect(result.data.metadata?.campaign).toBe('onboarding');
    }
  });

  it('rejects invalid URLs', () => {
    const result = BillingCheckoutSchema.safeParse({
      userId: 'user-1',
      plan: 'pro',
      successUrl: 'not-a-url',
      cancelUrl: 'https://example.com/cancel',
    });
    expect(result.success).toBe(false);
  });
});
