import type { ObservationSourceDescriptor } from "../runtime/contracts";
import type { ObservationSessionReplayLaunch } from "./sessionArchive";

export function recordedObservationSources(
  launch: ObservationSessionReplayLaunch | null,
): ObservationSourceDescriptor[] {
  if (!launch) return [];
  const spatial: ObservationSourceDescriptor = {
    id: "recorded.spatial.primary",
    sourceId: "recorded.spatial.primary",
    semanticChannelId: "spatial.point-cloud.recorded",
    label: "Сохранённая пространственная сцена",
    description: "Облако точек и траектория из записи Rerun",
    modality: "point-cloud",
    role: "primary",
    availability: "available",
    transport: "recording",
    endpointLabel: "RRD · session_time",
    previewUrl: launch.sourceUrl,
    delivery: null,
    activation: null,
    provider: {
      pluginId: "missioncore.session-archive",
      pluginVersion: "1",
      modelId: "recorded-spatial",
      compatibilityProfileId: null,
    },
    binding: {},
    capabilities: {
      overlay: false,
      fullscreen: true,
      resizable: false,
      defaultVisible: true,
      timelineMode: "recorded",
      seekable: true,
      sessionRecording: true,
      clockId: launch.timeline,
      spatialRegistration: "native",
    },
  };
  const media = launch.mediaSources.map((source, index): ObservationSourceDescriptor => {
    const perception = source.id.startsWith("recorded.perception.");
    return {
      id: source.id,
      sourceId: source.id,
      semanticChannelId: perception
        ? "camera.perception.panoptic.recorded"
        : "camera.video.recorded",
      label: source.label,
      description: perception
        ? "Покадровая instance + semantic сегментация на общей временной шкале"
        : "Сохранённый видеоканал на общей временной шкале сессии",
      modality: "video",
      role: "auxiliary",
      availability: "available",
      transport: "recording",
      endpointLabel: "Сохранённая сессия",
      previewUrl: null,
      delivery: {
        id: `${launch.sessionId}:${source.id}`,
        kind: "recorded-fmp4-manifest",
        url: source.manifestUrl,
        mediaType: source.mediaType,
        manifestGenerationSha256: source.manifestGenerationSha256,
        byteLength: source.byteLength,
        timelineStartSeconds: source.timelineStartSeconds,
        timelineEndSeconds: source.timelineEndSeconds,
      },
      activation: null,
      provider: {
        pluginId: "missioncore.session-archive",
        pluginVersion: "1",
        modelId: perception ? "recorded-panoptic-perception" : "recorded-media",
        compatibilityProfileId: null,
      },
      binding: {},
      capabilities: {
        overlay: true,
        fullscreen: true,
        resizable: true,
        // AI presentation belongs to the unified Rerun composition. Keep the
        // pre-rendered perception video as an explicit fallback instead of
        // opening it as a second, independently controlled stream.
        defaultVisible: !perception && index < 2,
        timelineMode: "recorded",
        seekable: true,
        sessionRecording: true,
        clockId: "session_time",
        spatialRegistration: perception ? "calibrated" : "unresolved",
      },
    };
  });
  return [spatial, ...media];
}
