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

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

describe('CreatePromoSchema', () => {
  it('accepts valid percent-off promo', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'SUMMER20',
      percentOff: 20,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.duration).toBe('once');
      expect(result.data.currency).toBe('usd');
    }
  });

  it('accepts valid amount-off promo', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'FLAT5',
      amountOff: 500,
      currency: 'usd',
    });
    expect(result.success).toBe(true);
  });

  it('accepts repeating duration with months', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'REPEAT3',
      percentOff: 10,
      duration: 'repeating',
      durationInMonths: 3,
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.duration).toBe('repeating');
      expect(result.data.durationInMonths).toBe(3);
    }
  });

  it('accepts promo with max redemptions and expiry', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'LIMITED',
      percentOff: 50,
      maxRedemptions: 100,
      expiresAt: '2026-12-31T23:59:59Z',
      createdBy: 'admin@lysnr.ai',
    });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.maxRedemptions).toBe(100);
      expect(result.data.createdBy).toBe('admin@lysnr.ai');
    }
  });

  it('rejects missing code', () => {
    const result = CreatePromoSchema.safeParse({ percentOff: 20 });
    expect(result.success).toBe(false);
  });

  it('rejects percent over 100', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'TOOMUCH',
      percentOff: 101,
    });
    expect(result.success).toBe(false);
  });

  it('rejects invalid duration', () => {
    const result = CreatePromoSchema.safeParse({
      code: 'BAD',
      percentOff: 10,
      duration: 'weekly',
    });
    expect(result.success).toBe(false);
  });
});
