import { describe, expect, it } from "vitest";
import { subPoint } from "../tgu-map-2d/geometry/passes";
import { checkVisibility, mergeConflictMaps, overlaps } from "./editorConflicts";
import type { Draft } from "./model/editorTypes";

const orbit = { spacecraftId: "56756" };
const centerMs = Date.UTC(2026, 4, 29, 12, 0, 0);
const visiblePoint = subPoint(orbit, centerMs);

describe("editorConflicts", () => {
  it("detects overlapping works and keeps non-overlapping works clean", () => {
    const conflicts = overlaps([
      { id: "shoot-1", kind: "shoot", label: "Съёмка", startMs: 1000, endMs: 5000, origin: "plan" },
      { id: "downlink-1", kind: "downlink", label: "Сброс", startMs: 4000, endMs: 7000, origin: "plan" },
      { id: "shoot-2", kind: "shoot", label: "Съёмка", startMs: 8000, endMs: 9000, origin: "plan" }
    ]);

    expect(conflicts).toEqual({
      "shoot-1": ['Пересечение по времени с "Сброс"'],
      "downlink-1": ['Пересечение по времени с "Съёмка"']
    });
  });

  it("reports target visibility conflicts only when the selected interval is not covered", () => {
    const draft: Draft = {
      spacecraftId: orbit.spacecraftId,
      works: [
        {
          id: "visible-shoot",
          kind: "shoot",
          label: "Съёмка",
          startMs: centerMs - 10_000,
          endMs: centerMs + 10_000,
          targetLat: visiblePoint.lat,
          targetLon: visiblePoint.lon,
          origin: "added"
        },
        {
          id: "hidden-shoot",
          kind: "shoot",
          label: "Съёмка",
          startMs: centerMs - 10_000,
          endMs: centerMs + 10_000,
          targetLat: -visiblePoint.lat,
          targetLon: visiblePoint.lon + 150,
          origin: "added"
        }
      ]
    };

    expect(checkVisibility(draft, { stations: [] })).toEqual({
      "hidden-shoot": ["Съёмка вне зоны видимости цели"]
    });
  });

  it("checks downlink station visibility and missing station coordinates", () => {
    const draft: Draft = {
      spacecraftId: orbit.spacecraftId,
      works: [
        {
          id: "visible-downlink",
          kind: "downlink",
          label: "Сброс",
          startMs: centerMs - 10_000,
          endMs: centerMs + 10_000,
          stationId: "KPP-1",
          origin: "added"
        },
        {
          id: "unknown-downlink",
          kind: "downlink",
          label: "Сброс",
          startMs: centerMs - 10_000,
          endMs: centerMs + 10_000,
          stationId: "KPP-2",
          origin: "added"
        }
      ]
    };

    expect(checkVisibility(draft, {
      stations: [
        { id: "KPP-1", name: "KPP-1", lat: visiblePoint.lat, lon: visiblePoint.lon },
        { id: "KPP-2", name: "KPP-2" }
      ]
    })).toEqual({
      "unknown-downlink": ["Нет координат станции для проверки видимости"]
    });
  });

  it("merges live and visibility conflict maps", () => {
    expect(mergeConflictMaps({ "work-1": ["A"] }, { "work-1": ["B"], "work-2": ["C"] })).toEqual({
      "work-1": ["A", "B"],
      "work-2": ["C"]
    });
  });
});
