/**
 * Unit tests for subscriptions module — types + validation.
 */

import { describe, it, expect } from 'vitest';
import {
  CreateSubscriptionSchema,
  UpdateSubscriptionSchema,
  CreatePaymentSchema,
} from './types.js';

describe('CreateSubscriptionSchema', () => {
  it('accepts valid input with required fields', () => {
    const result = CreateSubscriptionSchema.safeParse({
      userId: 'user_123',
      plan: 'pro',
      monthlyPrice: 9.99,
      tokensIncluded: 100000,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.status).toBe('active');
    }
  });

  it('accepts trialing status with trial days', () => {
    const result = CreateSubscriptionSchema.safeParse({
      userId: 'user_123',
      plan: 'enterprise',
      monthlyPrice: 29.99,
      tokensIncluded: 1000000,
      status: 'trialing',
      trialDays: 14,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.trialDays).toBe(14);
    }
  });

  it('rejects invalid plan', () => {
    const result = CreateSubscriptionSchema.safeParse({
      userId: 'user_123',
      plan: 'premium',
      monthlyPrice: 19.99,
      tokensIncluded: 50000,
    });
    expect(result.success).toBe(false);
  });

  it('rejects missing userId', () => {
    const result = CreateSubscriptionSchema.safeParse({
      plan: 'pro',
      monthlyPrice: 9.99,
      tokensIncluded: 100000,
    });
    expect(result.success).toBe(false);
  });

  it('rejects negative price', () => {
    const result = CreateSubscriptionSchema.safeParse({
      userId: 'user_123',
      plan: 'pro',
      monthlyPrice: -1,
      tokensIncluded: 100000,
    });
    expect(result.success).toBe(false);
  });
});

describe('UpdateSubscriptionSchema', () => {
  it('accepts partial updates', () => {
    const result = UpdateSubscriptionSchema.safeParse({
      plan: 'enterprise',
      cancelAtPeriodEnd: true,
    });
    expect(result.success).toBe(true);
  });

  it('accepts empty object', () => {
    const result = UpdateSubscriptionSchema.safeParse({});
    expect(result.success).toBe(true);
  });
});

describe('CreatePaymentSchema', () => {
  it('accepts valid payment', () => {
    const result = CreatePaymentSchema.safeParse({
      userId: 'user_123',
      amount: 999,
      status: 'succeeded',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.currency).toBe('usd');
      expect(result.data.method).toBe('card');
    }
  });

  it('rejects invalid status', () => {
    const result = CreatePaymentSchema.safeParse({
      userId: 'user_123',
      amount: 999,
      status: 'completed',
    });
    expect(result.success).toBe(false);
  });
});
