import { describe, it, expect } from 'vitest';
import {
  CreateFreshnessRuleSchema,
  UpdateFreshnessRuleSchema,
  FreshnessRuleQuerySchema,
} from './freshness-rule.schemas.js';

describe('FreshnessRule Schemas', () => {
  describe('CreateFreshnessRuleSchema', () => {
    const valid = {
      category: 'dairy',
      storageLocation: 'fridge',
      shelfLifeDays: 14,
      openedLifeDays: 7,
    };

    it('accepts valid input', () => {
      const result = CreateFreshnessRuleSchema.parse(valid);
      expect(result).toMatchObject(valid);
      expect(result.spoilageSignsToCheck).toEqual([]);
    });

    it('accepts all optional fields', () => {
      const full = {
        ...valid,
        freezerLifeDays: 90,
        spoilageSignsToCheck: ['smell', 'discoloration'],
        tips: 'Keep sealed tightly',
      };
      expect(CreateFreshnessRuleSchema.parse(full)).toMatchObject(full);
    });

    it('rejects missing category', () => {
      const { category: _, ...rest } = valid;
      expect(() => CreateFreshnessRuleSchema.parse(rest)).toThrow();
    });

    it('rejects invalid category', () => {
      expect(() => CreateFreshnessRuleSchema.parse({ ...valid, category: 'candy' })).toThrow();
    });

    it('rejects zero shelfLifeDays', () => {
      expect(() => CreateFreshnessRuleSchema.parse({ ...valid, shelfLifeDays: 0 })).toThrow();
    });

    it('rejects zero openedLifeDays', () => {
      expect(() => CreateFreshnessRuleSchema.parse({ ...valid, openedLifeDays: 0 })).toThrow();
    });
  });

  describe('UpdateFreshnessRuleSchema', () => {
    it('accepts partial update', () => {
      const result = UpdateFreshnessRuleSchema.parse({ shelfLifeDays: 10 });
      expect(result.shelfLifeDays).toBe(10);
    });

    it('accepts empty object', () => {
      expect(UpdateFreshnessRuleSchema.parse({})).toEqual({});
    });
  });

  describe('FreshnessRuleQuerySchema', () => {
    it('applies defaults', () => {
      const result = FreshnessRuleQuerySchema.parse({});
      expect(result.limit).toBe(50);
    });

    it('accepts all filters', () => {
      const input = {
        category: 'meat',
        storageLocation: 'fridge',
        cursor: 'abc',
        limit: 25,
      };
      expect(FreshnessRuleQuerySchema.parse(input)).toMatchObject(input);
    });
  });
});
