/**
 * Unit tests for licenses module — types + validation + key generation.
 */

import { describe, it, expect } from 'vitest';
import { GenerateLicenseSchema, ActivateLicenseSchema, DeactivateLicenseSchema } from './types.js';

describe('GenerateLicenseSchema', () => {
  it('accepts valid input with defaults', () => {
    const result = GenerateLicenseSchema.safeParse({
      userId: 'user_123',
      plan: 'pro',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.maxDevices).toBe(3);
      expect(result.data.expiresAt).toBeNull();
    }
  });

  it('accepts custom max devices', () => {
    const result = GenerateLicenseSchema.safeParse({
      userId: 'user_123',
      plan: 'enterprise',
      maxDevices: 10,
      expiresAt: '2027-01-01T00:00:00Z',
    });
    expect(result.success).toBe(true);
  });

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

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

describe('ActivateLicenseSchema', () => {
  it('accepts valid input', () => {
    const result = ActivateLicenseSchema.safeParse({
      key: 'LYSNR-AB12-CD34-EF56',
      deviceId: 'device_mac_001',
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty key', () => {
    const result = ActivateLicenseSchema.safeParse({
      key: '',
      deviceId: 'device_mac_001',
    });
    expect(result.success).toBe(false);
  });
});

describe('DeactivateLicenseSchema', () => {
  it('accepts valid input', () => {
    const result = DeactivateLicenseSchema.safeParse({
      key: 'LYSNR-AB12-CD34-EF56',
      deviceId: 'device_mac_001',
    });
    expect(result.success).toBe(true);
  });
});
