import { Given, Then } from "@badeball/cypress-cucumber-preprocessor";

/** Bare `IngredientView` — only `key` drives the page's `catalog.ingredients.*` lookup. */
function ingredient(key: string) {
  return {
    id: 1,
    key,
    icon: "VEGETABLE",
    category: "freshProduce",
    subcategory: "vegetables",
    reproducible: false,
    allergens: [],
    diets: [],
  };
}

Given(
  "the planning for this week has recipes {string} and {string}",
  (first: string, second: string) => {
    cy.intercept("GET", /\/planning\?/, {
      statusCode: 200,
      body: {
        id: 1,
        startDate: "2026-08-17T00:00:00.000Z",
        finishDate: "2026-08-23T00:00:00.000Z",
        items: [
          {
            id: 1,
            weekDay: "lundi",
            meal: "dejeuner",
            portions: 4,
            recipe: { id: 1, name: first },
          },
          { id: 2, weekDay: "mardi", meal: "diner", portions: 4, recipe: { id: 2, name: second } },
        ],
      },
    });
  },
);

Given(
  "the cooking plan for {string} pools {string} of {string} across both recipes",
  (date: string, _techniqueLabel: string, _ingredientLabel: string) => {
    // The page composes the headline itself from the technique/ingredient
    // *keys* via i18n — `chop`→"Hacher", `onion`→"Oignon" — so the fixture
    // carries keys; the `Then` step checks the rendered French labels the
    // feature line names.
    cy.intercept("GET", `**/cooking-session?date=${date}`, {
      statusCode: 200,
      body: {
        startDate: `${date}T00:00:00.000Z`,
        finishDate: "2026-08-23T00:00:00.000Z",
        recipes: [
          { recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
          { recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
        ],
        phases: [
          {
            index: 0,
            kind: "mise-en-place",
            background: [],
            tasks: [
              {
                id: "prep:chop:onion",
                kind: "merged-prep",
                technique: { id: 1, key: "chop" },
                description: null,
                ingredients: [
                  {
                    ingredient: ingredient("onion"),
                    quantity: 5,
                    unit: { id: 2, key: "piece", type: "COUNT", toBaseFactor: 1 },
                  },
                ],
                utensils: [],
                sourceRecipes: [
                  { recipeId: 1, name: "Soupe à l'oignon", portions: 4 },
                  { recipeId: 2, name: "Tarte à l'oignon", portions: 4 },
                ],
                originalSteps: [],
              },
            ],
          },
        ],
      },
    });
  },
);

Then(
  "the pooled prep task should mention {string} and {string}",
  (techniqueLabel: string, ingredientLabel: string) => {
    cy.get(".cooking-task--merged-prep")
      .should("contain.text", techniqueLabel)
      .and("contain.text", ingredientLabel);
  },
);

Then("the pooled prep task should be flagged as shared", () => {
  cy.get(".cooking-task--merged-prep").contains("Mutualisé").should("be.visible");
});
