/**
 * Unit tests for plans module — types + validation + defaults.
 */

import { describe, it, expect } from 'vitest';
import { CreatePlanSchema, UpdatePlanSchema, DEFAULT_PLANS } from './types.js';

describe('DEFAULT_PLANS', () => {
  it('has 3 plans', () => {
    expect(DEFAULT_PLANS).toHaveLength(3);
  });

  it('has free, pro, enterprise', () => {
    const names = DEFAULT_PLANS.map(p => p.name);
    expect(names).toEqual(['free', 'pro', 'enterprise']);
  });

  it('free plan has correct limits', () => {
    const free = DEFAULT_PLANS.find(p => p.name === 'free')!;
    expect(free.price).toBe(0);
    expect(free.tokens).toBe(10_000);
    expect(free.words).toBe(5_000);
    expect(free.dictations).toBe(100);
  });

  it('pro plan has correct price', () => {
    const pro = DEFAULT_PLANS.find(p => p.name === 'pro')!;
    expect(pro.price).toBe(9.99);
    expect(pro.tokens).toBe(100_000);
  });

  it('enterprise plan has correct limits', () => {
    const ent = DEFAULT_PLANS.find(p => p.name === 'enterprise')!;
    expect(ent.price).toBe(29.99);
    expect(ent.tokens).toBe(1_000_000);
  });
});

describe('CreatePlanSchema', () => {
  it('accepts valid plan', () => {
    const result = CreatePlanSchema.safeParse({
      name: 'team',
      displayName: 'Team',
      price: 19.99,
      tokens: 500_000,
      words: 250_000,
      dictations: 25_000,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.active).toBe(true);
      expect(result.data.features).toEqual([]);
    }
  });

  it('rejects missing name', () => {
    const result = CreatePlanSchema.safeParse({
      displayName: 'Team',
      price: 19.99,
      tokens: 500_000,
      words: 250_000,
      dictations: 25_000,
    });
    expect(result.success).toBe(false);
  });
});

describe('UpdatePlanSchema', () => {
  it('accepts partial updates', () => {
    const result = UpdatePlanSchema.safeParse({ price: 14.99, active: false });
    expect(result.success).toBe(true);
  });

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