import { describe, it, expect } from 'vitest';
import { CreateRecipeSchema, ScaleRecipeSchema, RecipeQuerySchema } from './recipe.schemas.js';

const validIngredient = {
  productId: 'prod-1',
  productName: 'Chicken Breast',
  quantity: 150,
  unit: 'g',
};

const validStep = { order: 1, instruction: 'Cook the chicken.' };

describe(CreateRecipeSchema.name, () => {
  it('accepts a minimal valid recipe', () => {
    const result = CreateRecipeSchema.safeParse({
      name: 'Grilled Chicken',
      servings: 2,
      ingredients: [validIngredient],
      steps: [validStep],
    });
    expect(result.success).toBe(true);
  });

  it('accepts imperial units in ingredients', () => {
    const result = CreateRecipeSchema.safeParse({
      name: 'Test Recipe',
      servings: 1,
      ingredients: [{ ...validIngredient, quantity: 1, unit: 'cup' }],
      steps: [validStep],
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty ingredients', () => {
    const result = CreateRecipeSchema.safeParse({
      name: 'Empty',
      servings: 2,
      ingredients: [],
      steps: [validStep],
    });
    expect(result.success).toBe(false);
  });

  it('rejects servings < 1', () => {
    const result = CreateRecipeSchema.safeParse({
      name: 'Bad',
      servings: 0,
      ingredients: [validIngredient],
      steps: [],
    });
    expect(result.success).toBe(false);
  });

  it('defaults isFavorite to false', () => {
    const result = CreateRecipeSchema.safeParse({
      name: 'Test',
      servings: 1,
      ingredients: [validIngredient],
      steps: [],
    });
    expect(result.success).toBe(true);
    if (result.success) expect(result.data.isFavorite).toBe(false);
  });
});

describe(ScaleRecipeSchema.name, () => {
  it('accepts valid target servings', () => {
    expect(ScaleRecipeSchema.safeParse({ targetServings: 4 }).success).toBe(true);
  });

  it('rejects zero servings', () => {
    expect(ScaleRecipeSchema.safeParse({ targetServings: 0 }).success).toBe(false);
  });
});

describe(RecipeQuerySchema.name, () => {
  it('defaults limit to 20', () => {
    const result = RecipeQuerySchema.safeParse({});
    expect(result.success).toBe(true);
    if (result.success) expect(result.data.limit).toBe(20);
  });

  it('clamps limit to 100', () => {
    const result = RecipeQuerySchema.safeParse({ limit: '200' });
    expect(result.success).toBe(false);
  });
});
