import { describe, it, expect } from 'vitest';
import { DosageUnit } from '../enums/medicine.enums.js';
import {
  CreateMedicinePriceRecordSchema,
  MedicinePriceHistoryQuerySchema,
  MedicinePriceAnalyticsQuerySchema,
} from './medicine-price.schemas.js';

const validInput = {
  medicineProductId: 'prod-1',
  medicineId: 'med-1',
  storeId: 'store-1',
  price: 9.99,
  currency: 'USD',
  quantity: 30,
  unit: DosageUnit.TABLET,
};

describe('CreateMedicinePriceRecordSchema', () => {
  it('accepts valid input', () => {
    expect(CreateMedicinePriceRecordSchema.safeParse(validInput).success).toBe(true);
  });

  it('defaults isInsurancePrice to false', () => {
    const result = CreateMedicinePriceRecordSchema.parse(validInput);
    expect(result.isInsurancePrice).toBe(false);
  });

  it('accepts optional date as ISO datetime', () => {
    const result = CreateMedicinePriceRecordSchema.safeParse({
      ...validInput,
      date: '2026-01-15T00:00:00.000Z',
    });
    expect(result.success).toBe(true);
  });

  it('rejects non-positive price', () => {
    expect(CreateMedicinePriceRecordSchema.safeParse({ ...validInput, price: 0 }).success).toBe(
      false,
    );
  });

  it('rejects non-positive quantity', () => {
    expect(CreateMedicinePriceRecordSchema.safeParse({ ...validInput, quantity: -1 }).success).toBe(
      false,
    );
  });

  it('rejects invalid unit', () => {
    expect(
      CreateMedicinePriceRecordSchema.safeParse({ ...validInput, unit: 'spoon' }).success,
    ).toBe(false);
  });
});

describe('MedicinePriceHistoryQuerySchema', () => {
  it('applies default limit', () => {
    const result = MedicinePriceHistoryQuerySchema.parse({});
    expect(result.limit).toBe(20);
  });

  it('accepts date range filters', () => {
    const result = MedicinePriceHistoryQuerySchema.parse({
      startDate: '2026-01-01T00:00:00.000Z',
      endDate: '2026-03-31T00:00:00.000Z',
    });
    expect(result.startDate).toBeTruthy();
    expect(result.endDate).toBeTruthy();
  });
});

describe('MedicinePriceAnalyticsQuerySchema', () => {
  it('defaults to month period', () => {
    const result = MedicinePriceAnalyticsQuerySchema.parse({});
    expect(result.period).toBe('month');
  });

  it.each(['month', 'quarter', 'year'])('accepts period %s', (period) => {
    expect(MedicinePriceAnalyticsQuerySchema.safeParse({ period }).success).toBe(true);
  });

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