import { describe, it, expect } from 'vitest';
import { CabinetEventType } from '../enums/cabinet-event.enums.js';
import {
  CabinetEventQuerySchema,
  SpendingSummaryQuerySchema,
  DiscardCabinetItemSchema,
} from './cabinet-event.schemas.js';

describe('CabinetEventQuerySchema', () => {
  it('applies default limit', () => {
    const result = CabinetEventQuerySchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.limit).toBe(20);
    }
  });

  it('accepts all filters', () => {
    const result = CabinetEventQuerySchema.safeParse({
      medicineId: 'med-1',
      eventType: CabinetEventType.PURCHASED,
      startDate: '2026-01-01T00:00:00.000Z',
      endDate: '2026-12-31T00:00:00.000Z',
      cursor: 'abc',
      limit: 50,
    });
    expect(result.success).toBe(true);
  });

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

  it('rejects limit over 100', () => {
    const result = CabinetEventQuerySchema.safeParse({ limit: 101 });
    expect(result.success).toBe(false);
  });
});

describe('SpendingSummaryQuerySchema', () => {
  it('defaults period to month', () => {
    const result = SpendingSummaryQuerySchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.period).toBe('month');
    }
  });

  it('accepts quarter period', () => {
    const result = SpendingSummaryQuerySchema.safeParse({ period: 'quarter' });
    expect(result.success).toBe(true);
  });

  it('accepts year period', () => {
    const result = SpendingSummaryQuerySchema.safeParse({ period: 'year' });
    expect(result.success).toBe(true);
  });

  it('rejects invalid period', () => {
    const result = SpendingSummaryQuerySchema.safeParse({ period: 'week' });
    expect(result.success).toBe(false);
  });

  it('accepts optional medicineId and date range', () => {
    const result = SpendingSummaryQuerySchema.safeParse({
      medicineId: 'med-1',
      startDate: '2026-01-01T00:00:00.000Z',
      endDate: '2026-06-30T00:00:00.000Z',
    });
    expect(result.success).toBe(true);
  });
});

describe('DiscardCabinetItemSchema', () => {
  it('accepts valid input', () => {
    const result = DiscardCabinetItemSchema.safeParse({
      reason: 'expired',
      notes: 'Found during cleanup',
    });
    expect(result.success).toBe(true);
  });

  it('accepts reason only', () => {
    const result = DiscardCabinetItemSchema.safeParse({ reason: 'damaged' });
    expect(result.success).toBe(true);
  });

  it('rejects empty reason', () => {
    const result = DiscardCabinetItemSchema.safeParse({ reason: '' });
    expect(result.success).toBe(false);
  });

  it('rejects missing reason', () => {
    const result = DiscardCabinetItemSchema.safeParse({});
    expect(result.success).toBe(false);
  });

  it('trims reason', () => {
    const result = DiscardCabinetItemSchema.safeParse({ reason: '  expired  ' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.reason).toBe('expired');
    }
  });

  it('rejects reason over 500 chars', () => {
    const result = DiscardCabinetItemSchema.safeParse({ reason: 'x'.repeat(501) });
    expect(result.success).toBe(false);
  });
});
