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

import { describe, it, expect } from 'vitest';
import { CreateInvitationSchema, UpdateInvitationSchema, RedeemInvitationSchema } from './types.js';

describe('CreateInvitationSchema', () => {
  it('accepts valid input with required fields', () => {
    const result = CreateInvitationSchema.safeParse({
      code: 'WELCOME2026',
      createdBy: 'admin@lysnr.ai',
      grantPlan: 'pro',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.code).toBe('WELCOME2026');
      expect(result.data.grantPlan).toBe('pro');
      expect(result.data.grantTrialDays).toBe(0);
      expect(result.data.bonusTokens).toBe(0);
      expect(result.data.maxUses).toBe(1);
      expect(result.data.expiresAt).toBeNull();
    }
  });

  it('accepts valid input with all fields', () => {
    const result = CreateInvitationSchema.safeParse({
      code: 'VIP-ENT',
      createdBy: 'admin@lysnr.ai',
      grantPlan: 'enterprise',
      grantTrialDays: 30,
      bonusTokens: 5000,
      maxUses: 50,
      expiresAt: '2026-12-31T00:00:00Z',
      description: 'VIP enterprise trial',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.grantTrialDays).toBe(30);
      expect(result.data.bonusTokens).toBe(5000);
      expect(result.data.maxUses).toBe(50);
    }
  });

  it('rejects missing code', () => {
    const result = CreateInvitationSchema.safeParse({
      createdBy: 'admin@lysnr.ai',
      grantPlan: 'pro',
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid grantPlan', () => {
    const result = CreateInvitationSchema.safeParse({
      code: 'TEST',
      createdBy: 'admin',
      grantPlan: 'free',
    });
    expect(result.success).toBe(false);
  });

  it('rejects code shorter than 3 chars', () => {
    const result = CreateInvitationSchema.safeParse({
      code: 'AB',
      createdBy: 'admin',
      grantPlan: 'pro',
    });
    expect(result.success).toBe(false);
  });

  it('rejects negative bonusTokens', () => {
    const result = CreateInvitationSchema.safeParse({
      code: 'TEST',
      createdBy: 'admin',
      grantPlan: 'pro',
      bonusTokens: -1,
    });
    expect(result.success).toBe(false);
  });
});

describe('UpdateInvitationSchema', () => {
  it('accepts partial updates', () => {
    const result = UpdateInvitationSchema.safeParse({
      description: 'Updated description',
      status: 'disabled',
    });
    expect(result.success).toBe(true);
  });

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

  it('rejects invalid status', () => {
    const result = UpdateInvitationSchema.safeParse({ status: 'deleted' });
    expect(result.success).toBe(false);
  });
});

describe('RedeemInvitationSchema', () => {
  it('accepts valid redeem input', () => {
    const result = RedeemInvitationSchema.safeParse({
      code: 'WELCOME2026',
      userId: 'user_123',
    });
    expect(result.success).toBe(true);
  });

  it('rejects missing userId', () => {
    const result = RedeemInvitationSchema.safeParse({ code: 'WELCOME2026' });
    expect(result.success).toBe(false);
  });

  it('rejects empty code', () => {
    const result = RedeemInvitationSchema.safeParse({ code: '', userId: 'user_123' });
    expect(result.success).toBe(false);
  });
});
