export type BackendStatus = "unconfigured" | "checking" | "online" | "degraded" | "offline";

export type RuntimePhase =
  | "unconfigured"
  | "idle"
  | "configuring"
  | "connected"
  | "starting"
  | "streaming"
  | "replaying"
  | "stopping"
  | "error";

export type SourceMode = "idle" | "live" | "replay";

export interface ViewerSettings {
  point_size: number;
  color_mode: "intensity" | "height" | "distance" | "rgb" | "class";
  palette: "turbo" | "viridis" | "plasma" | "grayscale" | "custom";
  custom_color: string;
  accumulation_seconds: number;
  show_points: boolean;
  show_trajectory: boolean;
  show_grid: boolean;
  show_detections_2d: boolean;
  show_segmentation: boolean;
  show_cuboids_3d: boolean;
}

export interface StreamMetrics {
  latencyMs?: number | null;
  frameRateHz?: number | null;
  pointCount?: number | null;
  droppedPreviewFrames?: number | null;
  aiLatencyMs?: number | null;
  aiFrameRateHz?: number | null;
  aiDroppedFrames?: number | null;
  aiStaleMs?: number | null;
  elapsedSeconds?: number | null;
  routeDistanceMeters?: number | null;
  speedMetersPerSecond?: number | null;
}

export interface ActiveDeviceSnapshot {
  pluginId: string;
  modelId: string;
  displayName: string;
  instanceId?: string | null;
  endpointLabel?: string | null;
}

export interface RuntimeDeviceSessionSnapshot {
  sessionId: string;
  deviceId: string;
  compatibilityProfileId?: string | null;
  connectivity?: string | null;
}

export interface RuntimeAcquisitionSnapshot {
  acquisitionId: string;
  deviceId: string;
  deviceSessionId: string;
  compatibilityProfileId: string;
  controlMode: string;
  state: string;
  stateRevision: number;
  operatorInstructions: readonly string[];
  cleanupPending?: boolean;
}

export interface RuntimeOperationSnapshot {
  operationId: string;
  action: string;
  status: string;
  stageCode?: string | null;
  messageCode?: string | null;
}

export interface SpatialSourceDescriptor {
  id: string;
  url: string;
  label: string;
  kind: "rerun-grpc" | "rrd" | "other";
}

export type ObservationSourceModality = "point-cloud" | "video" | "image" | "depth";

export type ObservationSourceAvailability =
  | "unverified"
  | "declared"
  | "available"
  | "connecting"
  | "streaming"
  | "degraded"
  | "unavailable"
  | "error";

export type ObservationTimelineMode = "live-only" | "buffered" | "recorded";

export interface ObservationSourceProvider {
  pluginId: string;
  pluginVersion: string;
  modelId: string;
  compatibilityProfileId?: string | null;
}

export interface ObservationSourceBinding {
  deviceId?: string | null;
  deviceSessionId?: string | null;
  acquisitionId?: string | null;
}

export interface ObservationSourceCapabilities {
  overlay: boolean;
  fullscreen: boolean;
  resizable: boolean;
  defaultVisible: boolean;
  timelineMode: ObservationTimelineMode;
  seekable: boolean;
  sessionRecording: boolean;
  clockId?: string | null;
  spatialRegistration: "native" | "calibrated" | "unresolved" | "not-applicable";
}

export type ObservationSourceDelivery =
  | {
      id: string;
      kind: "mse-fmp4-websocket";
      url: string;
      mediaType: string;
    }
  | {
      id: string;
      kind: "video-url" | "image-url";
      url: string;
      mediaType?: string | null;
    }
  | {
      id: string;
      kind: "recorded-fmp4-manifest";
      url: string;
      mediaType: "video/mp4";
      manifestGenerationSha256: string;
      byteLength: number;
      timelineStartSeconds: number;
      timelineEndSeconds: number;
    };

export interface ObservationSourceActivation {
  groupId: string;
  maxActive: number;
  selected: boolean;
  controllable: boolean;
}

export interface ObservationSourceDescriptor {
  id: string;
  sourceId: string;
  semanticChannelId: string;
  label: string;
  description: string;
  modality: ObservationSourceModality;
  role: "primary" | "auxiliary";
  availability: ObservationSourceAvailability;
  transport: "rerun-grpc" | "rtsp" | "websocket" | "recording" | "other";
  endpointLabel?: string | null;
  previewUrl?: string | null;
  delivery?: ObservationSourceDelivery | null;
  activation?: ObservationSourceActivation | null;
  provider: ObservationSourceProvider;
  binding: ObservationSourceBinding;
  capabilities: ObservationSourceCapabilities;
}

export interface ObservationTimelineSnapshot {
  mode: ObservationTimelineMode;
  seekable: boolean;
  sessionRecording: boolean;
  synchronization: "host-arrival-best-effort" | "shared-clock" | "frame-accurate";
  range: { startSeconds: number; endSeconds: number } | null;
}

export interface MissionRuntimeState {
  phase: RuntimePhase;
  message?: string | null;
  activeDevice?: ActiveDeviceSnapshot | null;
  deviceSession?: RuntimeDeviceSessionSnapshot | null;
  acquisition?: RuntimeAcquisitionSnapshot | null;
  operations?: readonly RuntimeOperationSnapshot[];
  spatialSource?: SpatialSourceDescriptor | null;
  observationSources?: readonly ObservationSourceDescriptor[];
  observationTimeline?: ObservationTimelineSnapshot;
  viewerSettings?: ViewerSettings | null;
  sourceMode: SourceMode;
  metrics?: StreamMetrics;
}

export interface MissionRuntimeController {
  state: MissionRuntimeState | null;
  backendStatus: BackendStatus;
  pendingAction: string | null;
  refresh: () => void | Promise<void>;
  updateViewerSettings: (settings: ViewerSettings) => Promise<boolean>;
  setObservationSourceActive?: (sourceId: string, active: boolean) => Promise<boolean>;
}
