import { describe, it, expect } from 'vitest';
import {
  CreateShoppingListSchema,
  AddShoppingItemSchema,
  UpdateShoppingItemSchema,
} from './shopping-list.schemas.js';
import { ServingUnit } from '../enums/product.enums.js';

describe('ShoppingList Schemas', () => {
  describe('CreateShoppingListSchema', () => {
    it('should accept lists with items', () => {
      const payload = {
        name: 'Weekly run',
        items: [
          { productId: 'p1', quantity: 2, unit: ServingUnit.PIECES },
          { customName: 'Apples', quantity: 10, unit: ServingUnit.PIECES },
        ],
      };
      const result = CreateShoppingListSchema.safeParse(payload);
      expect(result.success).toBe(true);
    });

    it('should default to empty items array', () => {
      const payload = { name: 'Mini list' };
      const result = CreateShoppingListSchema.safeParse(payload);
      expect(result.success).toBe(true);
      if (result.success) {
        expect(result.data.items).toEqual([]);
      }
    });
  });

  describe('AddShoppingItemSchema', () => {
    it('should pass with a productId', () => {
      const payload = { productId: 'prod-123', quantity: 1, unit: ServingUnit.MILLILITERS };
      const result = AddShoppingItemSchema.safeParse(payload);
      expect(result.success).toBe(true);
    });

    it('should pass with a customName', () => {
      const payload = { customName: 'Fresh fish', quantity: 1.5, unit: ServingUnit.GRAMS };
      const result = AddShoppingItemSchema.safeParse(payload);
      expect(result.success).toBe(true);
    });

    it('should fail if both are omitted', () => {
      const payload = { quantity: 5, unit: ServingUnit.PIECES };
      const result = AddShoppingItemSchema.safeParse(payload);
      expect(result.success).toBe(false);
    });
  });

  describe('UpdateShoppingItemSchema', () => {
    it('should accept partial updates', () => {
      const payload = { checked: true, actualPrice: 2.99 };
      const result = UpdateShoppingItemSchema.safeParse(payload);
      expect(result.success).toBe(true);
    });

    it('should reject negative prices on checkoff', () => {
      const payload = { actualPrice: -0.5 };
      const result = UpdateShoppingItemSchema.safeParse(payload);
      expect(result.success).toBe(false);
    });
  });
});
