import { describe, expect, it } from "vitest";
import type { MissionValidation } from "../../api/missionPlanningApi";
import { validationToConflicts } from "./editorValidation";
import type { EditorWork } from "./model/editorTypes";

function shoot(id: string, planModeId?: number): EditorWork {
  return {
    id,
    kind: "shoot",
    label: "Съёмка",
    startMs: 0,
    endMs: 1000,
    origin: "plan",
    planModeId
  };
}

describe("validationToConflicts", () => {
  it("maps backend conflicts onto editor work ids by planModeId", () => {
    const works = [shoot("survey-7", 7), shoot("survey-9", 9)];
    const validation: MissionValidation = {
      missionId: "m1",
      conflicts: [
        { modeId: 7, rule: "TIME_OVERLAP", severity: "error", message: "Пересечение по времени", relatedModeId: 9 },
        { modeId: 7, rule: "TARGET_VISIBILITY", severity: "warning", message: "Вне зоны видимости" }
      ]
    };

    const map = validationToConflicts(validation, works);

    expect(map["survey-7"]).toEqual(["Пересечение по времени", "Вне зоны видимости"]);
    expect(map["survey-9"]).toBeUndefined();
  });

  it("drops conflicts whose modeId has no matching work", () => {
    const works = [shoot("survey-7", 7)];
    const validation: MissionValidation = {
      missionId: "m1",
      conflicts: [{ modeId: 42, rule: "TIME_OVERLAP", severity: "error", message: "x" }]
    };

    expect(validationToConflicts(validation, works)).toEqual({});
  });

  it("returns an empty map for the stub (no conflicts)", () => {
    const works = [shoot("survey-7", 7)];
    expect(validationToConflicts({ missionId: "m1", conflicts: [] }, works)).toEqual({});
  });
});
