import { describe, it, expect } from 'vitest';
import { OrganizerFillStatus } from '../enums/regimen.enums.js';
import {
  OrganizerPreviewSchema,
  OrganizerFillSchema,
  OrganizerFillQuerySchema,
} from './organizer.schemas.js';

describe('OrganizerPreviewSchema', () => {
  it('accepts valid input', () => {
    const result = OrganizerPreviewSchema.safeParse({
      regimenId: 'reg-1',
      numberOfDays: 7,
    });
    expect(result.success).toBe(true);
  });

  it('defaults numberOfDays to 7', () => {
    const result = OrganizerPreviewSchema.safeParse({ regimenId: 'reg-1' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.numberOfDays).toBe(7);
    }
  });

  it('rejects missing regimenId', () => {
    const result = OrganizerPreviewSchema.safeParse({ numberOfDays: 7 });
    expect(result.success).toBe(false);
  });

  it('rejects numberOfDays over 90', () => {
    const result = OrganizerPreviewSchema.safeParse({
      regimenId: 'reg-1',
      numberOfDays: 91,
    });
    expect(result.success).toBe(false);
  });

  it('rejects numberOfDays of 0', () => {
    const result = OrganizerPreviewSchema.safeParse({
      regimenId: 'reg-1',
      numberOfDays: 0,
    });
    expect(result.success).toBe(false);
  });
});

describe('OrganizerFillSchema', () => {
  it('accepts valid input', () => {
    const result = OrganizerFillSchema.safeParse({
      regimenId: 'reg-1',
      numberOfDays: 7,
      allowPartial: true,
    });
    expect(result.success).toBe(true);
  });

  it('defaults allowPartial to false', () => {
    const result = OrganizerFillSchema.safeParse({ regimenId: 'reg-1' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.allowPartial).toBe(false);
    }
  });

  it('accepts optional notes', () => {
    const result = OrganizerFillSchema.safeParse({
      regimenId: 'reg-1',
      notes: 'Weekly fill',
    });
    expect(result.success).toBe(true);
  });

  it('rejects notes over 1000 chars', () => {
    const result = OrganizerFillSchema.safeParse({
      regimenId: 'reg-1',
      notes: 'x'.repeat(1001),
    });
    expect(result.success).toBe(false);
  });
});

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

  it('accepts status filter', () => {
    const result = OrganizerFillQuerySchema.safeParse({
      status: OrganizerFillStatus.COMPLETED,
    });
    expect(result.success).toBe(true);
  });

  it('accepts regimenId filter', () => {
    const result = OrganizerFillQuerySchema.safeParse({ regimenId: 'reg-1' });
    expect(result.success).toBe(true);
  });

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

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