import assert from "node:assert/strict";
import { after, before, test } from "node:test";

import { createServer } from "vite";

let server;
let parseLidarReplayCatalog;
let parseLidarReplayDetail;
let parseLidarGroundBenchmarkCatalog;
let parseLidarGroundFrame;
let parseLidarFieldReviewCatalog;
let parseLidarFieldReviewWindow;
let fetchLidarReplayCatalog;
let fetchLidarReplayDetail;
let fetchLidarGroundBenchmarks;
let fetchLidarGroundFrame;
let fetchLidarFieldReviews;
let fetchLidarFieldReviewWindow;
let LidarReplayContractError;
let workspaceById;

before(async () => {
  server = await createServer({
    appType: "custom",
    logLevel: "silent",
    server: { middlewareMode: true },
  });
  ({
    parseLidarReplayCatalog,
    parseLidarReplayDetail,
    parseLidarGroundBenchmarkCatalog,
    parseLidarGroundFrame,
    parseLidarFieldReviewCatalog,
    parseLidarFieldReviewWindow,
    fetchLidarReplayCatalog,
    fetchLidarReplayDetail,
    fetchLidarGroundBenchmarks,
    fetchLidarGroundFrame,
    fetchLidarFieldReviews,
    fetchLidarFieldReviewWindow,
    LidarReplayContractError,
  } = await server.ssrLoadModule("/src/core/lidar/replayQuality.ts"));
  ({ workspaceById } = await server.ssrLoadModule("/src/productModel.ts"));
});

after(async () => {
  await server?.close();
});

const packId = `lidar-replay-pack-${"a".repeat(64)}`;
const fieldReviewId = `lidar-field-review-${"d".repeat(64)}`;
const e10PackId = `e10-lidar-pack-${"e".repeat(64)}`;

function summary(overrides = {}) {
  return {
    pack_id: packId,
    session_id: "20260719T220917Z_viewer_live",
    profile_id: "xgrids-k1-lidar-replay-pack/v2",
    point_frames: 10,
    pose_frames: 12,
    points: 1234,
    mean_points_per_frame: 123.4,
    p95_frame_interval_ms: 98.7,
    pose_coverage_fraction: 0.9,
    equivalence_status: "passed",
    logical_content_sha256: "b".repeat(64),
    created_at_utc: "2026-07-25T00:00:00Z",
    authority: {
      commands_enabled: false,
      navigation_or_safety_accepted: false,
    },
    ...overrides,
  };
}

function distribution() {
  return {
    sample_count: 10,
    minimum: 1,
    mean: 2,
    p50: 2,
    p95: 3,
    maximum: 4,
  };
}

function catalog(overrides = {}) {
  return {
    schema_version: "missioncore.lidar-replay-pack-catalog/v1",
    configured: true,
    items: [summary()],
    valid_total: 1,
    invalid_total: 0,
    duplicate_total: 0,
    access: "read-only",
    ...overrides,
  };
}

function detail() {
  return {
    schema_version: "missioncore.lidar-replay-pack-detail/v1",
    access: "read-only",
    pack: summary({ created_at_utc: undefined }),
    quality: {
      schema_version: "missioncore.lidar-quality-report/v1",
      field_retention: {
        xyz_map: true,
        raw_xyz: true,
        raw_rgbi: true,
        intensity_low_byte: true,
        source_sequence: true,
        header_seq_stamp_scaler: true,
        host_epoch_ns: true,
        host_monotonic_ns: true,
      },
      point_count_per_frame: distribution(),
      point_frame_interval_ms: distribution(),
      intensity_0_255: distribution(),
      pose_binding: {
        threshold_ms: 100,
        covered_point_frames: 9,
        coverage_fraction: 0.9,
        nearest_delta_ms: distribution(),
      },
      limitations: ["vendor-mapped increment"],
    },
    equivalence: {
      schema_version: "missioncore.lidar-live-replay-equivalence/v1",
      status: "passed",
      arrays_compared: 24,
      array_mismatches: 0,
    },
    readiness: {
      stages: [
        {
          stage: "lidar-3d-detection",
          readiness: "degraded",
          reasons: ["pretrained-domain-expects-sensor-scan"],
        },
        {
          stage: "lidar-inertial-slam",
          readiness: "blocked",
          reasons: ["lio-requires-unregistered-sensor-scans"],
        },
      ],
    },
  };
}

function groundCatalog(overrides = {}) {
  const provider = (providerId, sourceCommit = undefined) => ({
    provider_id: providerId,
    source_commit: sourceCommit,
  });
  const branch = (providerValue, groundP50, latencyP95) => ({
    provider: providerValue,
    ground_fraction: {
      ...distribution(),
      p50: groundP50,
    },
    assigned_fraction: {
      ...distribution(),
      minimum: 1,
      mean: 1,
      p50: 1,
      p95: 1,
      maximum: 1,
    },
    latency_ms: {
      ...distribution(),
      p95: latencyP95,
    },
  });
  return {
    schema_version: "missioncore.lidar-ground-benchmark-catalog/v1",
    configured: true,
    valid_total: 1,
    invalid_total: 0,
    access: "read-only",
    items: [{
      benchmark_id: `ground-benchmark-${"c".repeat(64)}`,
      replay_pack_id: packId,
      session_id: "20260719T220917Z_viewer_live",
      status: "diagnostic-only",
      frames: 66,
      points: 226963,
      input_domain: {
        accepted: false,
        representation: "vendor-map-increment",
        physical_sensor_height_known: false,
        sensor_scan_geometry_known: false,
        normalization: {
          sensor_height_m: 1.27,
          map_vertical_origin_offset_m: 1.27,
          height_evidence: "operator-estimated",
        },
        reason: "Patchwork++ expects sensor-centric scans.",
      },
      labels: {
        status: "missing-independent-review",
        metrics_available: false,
      },
      current: branch(
        provider("missioncore-local-percentile-ground/v1"),
        0.183,
        10.7,
      ),
      candidate: branch(
        provider(
          "patchworkpp/v1.4.1",
          "3e6903a1d5537a4cc2ace897b0bbb98a92d6014c",
        ),
        0.005,
        0.29,
      ),
      comparison: {
        algorithm_to_algorithm_ground_iou: {
          ...distribution(),
          p50: 0.029,
        },
        ground_disagreement_fraction: {
          ...distribution(),
          p50: 0.179,
        },
        is_accuracy_metric: false,
      },
      decision: {
        status: "do-not-promote-on-current-vendor-map",
        production_promotion: false,
        reasons: ["candidate input domain is not accepted"],
        next_gate: "human-reviewed annotation subset",
      },
      created_at_utc: "2026-07-25T01:00:00Z",
    }],
    ...overrides,
  };
}

function groundFrame(overrides = {}) {
  return {
    schema_version: "missioncore.lidar-ground-frame/v1",
    benchmark_id: `ground-benchmark-${"c".repeat(64)}`,
    replay_pack_id: packId,
    session_id: "20260719T220917Z_viewer_live",
    frame_index: 0,
    frame_count: 2,
    capture_sequence: 42,
    point_count: 3,
    coordinate_frame: "map",
    distance_unit: "m",
    points_xyz_m: [
      [0, 0, 0],
      [1, 0, 0.1],
      [0, 1, 0.5],
    ],
    intensity_0_255: [10, 120, 255],
    masks: {
      current_ground: [1, 1, 0],
      current_assigned: [1, 1, 1],
      candidate_ground: [1, 0, 0],
      candidate_assigned: [1, 1, 1],
      disagreement: [0, 1, 0],
    },
    counts: {
      current_ground: 2,
      candidate_ground: 1,
      disagreement: 1,
    },
    access: "read-only",
    ground_truth: false,
    authority: {
      commands_enabled: false,
      navigation_or_safety_accepted: false,
    },
    ...overrides,
  };
}

function fieldReviewWindowSummary(overrides = {}) {
  return {
    index: 0,
    key: "intersection-facades",
    label: "Перекрёсток, дорога и фасады",
    start_seconds: 145,
    end_seconds: 151,
    midpoint_seconds: 148,
    source_lidar_samples: 56,
    source_point_count: 98538,
    display_point_count: 3,
    source_frame_start: 1097,
    source_frame_end: 1156,
    preview_source_frame_index: 1120,
    preview_session_seconds: 147.451857292,
    ...overrides,
  };
}

function fieldReviewCatalog(overrides = {}) {
  const provider = (providerId, sourceCommit = undefined) => ({
    provider_id: providerId,
    source_commit: sourceCommit,
  });
  const branch = (providerValue) => ({
    provider: providerValue,
    ground_fraction: distribution(),
    latency_ms: distribution(),
  });
  return {
    schema_version: "missioncore.lidar-field-review-catalog/v1",
    configured: true,
    valid_total: 1,
    invalid_total: 0,
    access: "read-only",
    items: [{
      review_id: fieldReviewId,
      display_name: "RAVNOVES00 · центральный городской интервал",
      session_id: "20260720T065719Z_viewer_live",
      source_pack_id: e10PackId,
      status: "diagnostic-only",
      source: {
        timeline_start_seconds: 135.365857292,
        timeline_end_seconds: 195.334857292,
        available_lidar_frames: 526,
        point_count: 1182292,
        representation: "legacy-e10-vendor-map-with-pose",
        intensity_available: false,
        raw_scan_accepted: false,
      },
      selection: {
        purpose: "operator-readable-central-urban-field-review",
        default_window_index: 0,
        accumulation: "per-source-frame masks accumulated in map frame",
        sampling: {
          method: "uniform-point-index-per-window",
          maximum_points_per_window: 80000,
        },
      },
      windows: [fieldReviewWindowSummary()],
      metrics: {
        source_samples: 56,
        current: branch(
          provider("missioncore-local-percentile-ground/v1"),
        ),
        candidate: branch(
          provider(
            "patchworkpp/v1.4.1",
            "3e6903a1d5537a4cc2ace897b0bbb98a92d6014c",
          ),
        ),
        comparison: {
          algorithm_to_algorithm_ground_iou: distribution(),
          ground_disagreement_fraction: distribution(),
          is_accuracy_metric: false,
        },
      },
      decision: {
        status: "visual-review-only",
        production_promotion: false,
        reasons: ["legacy derivative does not retain intensity"],
      },
      created_at_utc: "2026-07-25T02:00:00Z",
      ground_truth: false,
      authority: {
        commands_enabled: false,
        navigation_or_safety_accepted: false,
      },
    }],
    ...overrides,
  };
}

function fieldReviewWindow(overrides = {}) {
  return {
    schema_version: "missioncore.lidar-field-review-window/v1",
    review_id: fieldReviewId,
    display_name: "RAVNOVES00 · центральный городской интервал",
    session_id: "20260720T065719Z_viewer_live",
    source_pack_id: e10PackId,
    window_index: 0,
    window_count: 1,
    window: fieldReviewWindowSummary(),
    point_count: 3,
    coordinate_frame: "map",
    distance_unit: "m",
    intensity: {
      available: false,
      reason: "E10 derivative did not retain rgbi/intensity",
    },
    points_xyz_m: [
      [0, 0, 0],
      [1, 0, 0.1],
      [0, 1, 0.5],
    ],
    masks: {
      current_ground: [1, 1, 0],
      current_assigned: [1, 1, 1],
      candidate_ground: [1, 0, 0],
      candidate_assigned: [1, 1, 1],
      disagreement: [0, 1, 0],
    },
    counts: {
      current_ground: 2,
      candidate_ground: 1,
      disagreement: 1,
    },
    preview_url:
      `/api/v1/lidar/field-reviews/${fieldReviewId}/windows/0/preview`,
    access: "read-only",
    ground_truth: false,
    authority: {
      commands_enabled: false,
      navigation_or_safety_accepted: false,
    },
    ...overrides,
  };
}

function jsonResponse(payload, status = 200) {
  return new Response(JSON.stringify(payload), {
    status,
    headers: { "content-type": "application/json" },
  });
}

test("LiDAR catalog and detail decode only passed, path-free evidence", () => {
  const parsedCatalog = parseLidarReplayCatalog(catalog());
  const parsedDetail = parseLidarReplayDetail(detail());

  assert.equal(parsedCatalog.items[0].packId, packId);
  assert.equal(parsedDetail.quality.fieldRetention.raw_rgbi, true);
  assert.equal(parsedDetail.equivalence.arrayMismatches, 0);
  assert.equal(parsedDetail.stages[0].readiness, "degraded");
  assert.equal("path" in parsedCatalog.items[0], false);
});

test("LiDAR contract refuses replay that did not pass equivalence", () => {
  assert.throws(
    () => parseLidarReplayCatalog(catalog({
      items: [summary({ equivalence_status: "failed" })],
    })),
    LidarReplayContractError,
  );
});

test("ground benchmark stays diagnostic until input and labels are accepted", () => {
  const parsed = parseLidarGroundBenchmarkCatalog(groundCatalog());

  assert.equal(parsed.items[0].status, "diagnostic-only");
  assert.equal(parsed.items[0].inputDomain.accepted, false);
  assert.equal(parsed.items[0].labels.metricsAvailable, false);
  assert.equal(parsed.items[0].candidate.groundFraction.p50, 0.005);
  assert.equal(
    parsed.items[0].inputDomain.normalization.heightEvidence,
    "operator-estimated",
  );
  assert.equal(parsed.items[0].decision.productionPromotion, false);

  const promoted = groundCatalog();
  promoted.items[0].decision.production_promotion = true;
  assert.throws(
    () => parseLidarGroundBenchmarkCatalog(promoted),
    LidarReplayContractError,
  );
});

test("ground frame stays point-aligned, bounded and path-free", () => {
  const parsed = parseLidarGroundFrame(groundFrame());

  assert.equal(parsed.pointCount, 3);
  assert.deepEqual(parsed.pointsXyzM[1], [1, 0, 0.1]);
  assert.deepEqual(parsed.masks.disagreement, [0, 1, 0]);
  assert.equal(parsed.counts.currentGround, 2);
  assert.equal("path" in parsed, false);

  assert.throws(
    () => parseLidarGroundFrame(groundFrame({
      masks: {
        ...groundFrame().masks,
        disagreement: [0, 1],
      },
    })),
    LidarReplayContractError,
  );
  assert.throws(
    () => parseLidarGroundFrame(groundFrame({
      counts: {
        current_ground: 1,
        candidate_ground: 1,
        disagreement: 1,
      },
    })),
    LidarReplayContractError,
  );
  assert.throws(
    () => parseLidarGroundFrame(groundFrame({
      masks: {
        ...groundFrame().masks,
        disagreement: [0, 0, 0],
      },
      counts: {
        current_ground: 2,
        candidate_ground: 1,
        disagreement: 0,
      },
    })),
    LidarReplayContractError,
  );
});

test("field review identifies RAVNOVES00 source and stays visual-only", () => {
  const parsedCatalog = parseLidarFieldReviewCatalog(fieldReviewCatalog());
  const parsedWindow = parseLidarFieldReviewWindow(fieldReviewWindow());

  assert.equal(parsedCatalog.items[0].sourcePackId, e10PackId);
  assert.equal(parsedCatalog.items[0].source.availableLidarFrames, 526);
  assert.equal(parsedCatalog.items[0].selection.defaultWindowIndex, 0);
  assert.equal(parsedWindow.window.label, "Перекрёсток, дорога и фасады");
  assert.equal(parsedWindow.intensity0To255, null);
  assert.equal(parsedWindow.counts.disagreement, 1);
  assert.equal("path" in parsedWindow, false);

  const promoted = fieldReviewCatalog();
  promoted.items[0].decision.production_promotion = true;
  assert.throws(
    () => parseLidarFieldReviewCatalog(promoted),
    LidarReplayContractError,
  );
});

test("field-review window refuses forged preview and point masks", () => {
  assert.throws(
    () => parseLidarFieldReviewWindow(fieldReviewWindow({
      preview_url: "https://example.invalid/frame.jpg",
    })),
    LidarReplayContractError,
  );
  assert.throws(
    () => parseLidarFieldReviewWindow(fieldReviewWindow({
      masks: {
        ...fieldReviewWindow().masks,
        disagreement: [0, 0, 0],
      },
      counts: {
        current_ground: 2,
        candidate_ground: 1,
        disagreement: 0,
      },
    })),
    LidarReplayContractError,
  );
});

test("LiDAR fetchers use read-only endpoints and workspace is registered", async () => {
  const calls = [];
  const fetcher = async (input, init) => {
    calls.push({ input: String(input), method: init?.method });
    if (String(input).includes("ground-benchmarks")) {
      return String(input).includes("/frames/")
        ? jsonResponse(groundFrame())
        : jsonResponse(groundCatalog());
    }
    if (String(input).includes("field-reviews")) {
      return String(input).includes("/windows/")
        ? jsonResponse(fieldReviewWindow())
        : jsonResponse(fieldReviewCatalog());
    }
    return String(input).includes(packId)
      ? jsonResponse(detail())
      : jsonResponse(catalog());
  };
  const parsedCatalog = await fetchLidarReplayCatalog({ fetcher });
  const parsedDetail = await fetchLidarReplayDetail(packId, { fetcher });
  const ground = await fetchLidarGroundBenchmarks(packId, { fetcher });
  const frame = await fetchLidarGroundFrame(
    `ground-benchmark-${"c".repeat(64)}`,
    0,
    { fetcher },
  );
  const fieldCatalog = await fetchLidarFieldReviews({ fetcher });
  const fieldWindow = await fetchLidarFieldReviewWindow(
    fieldReviewId,
    0,
    { fetcher },
  );

  assert.equal(parsedCatalog.validTotal, 1);
  assert.equal(parsedDetail.pack.packId, packId);
  assert.equal(ground.validTotal, 1);
  assert.equal(frame.pointCount, 3);
  assert.equal(fieldCatalog.items[0].windows[0].sourceLidarSamples, 56);
  assert.equal(fieldWindow.previewUrl.endsWith("/preview"), true);
  assert.deepEqual(calls, [
    { input: "/api/v1/lidar/replay-packs?limit=50", method: "GET" },
    { input: `/api/v1/lidar/replay-packs/${packId}`, method: "GET" },
    {
      input: `/api/v1/lidar/ground-benchmarks?pack_id=${packId}&limit=20`,
      method: "GET",
    },
    {
      input: `/api/v1/lidar/ground-benchmarks/ground-benchmark-${"c".repeat(64)}/frames/0`,
      method: "GET",
    },
    {
      input: "/api/v1/lidar/field-reviews?limit=10",
      method: "GET",
    },
    {
      input: `/api/v1/lidar/field-reviews/${fieldReviewId}/windows/0`,
      method: "GET",
    },
  ]);
  assert.equal(workspaceById("lidar-quality").root, "fleet");
  assert.equal(workspaceById("lidar-quality").kind, "lidar-quality");
});
