import type { Map as MapLibreMap } from "maplibre-gl";
import type { WaterNetworkSourceId } from "./map/sources";

export type DetailFeature = {
  id: string;
  layer: WaterNetworkSourceId;
  title: string;
  subtitle: string;
  properties: Record<string, unknown>;
};

export type WorkbenchMode = "simulation" | "feature-detail";

export type WorkbenchScenario = {
  id: string;
  name: string;
  description: string;
  status: "draft" | "active" | "review";
};

export type WorkbenchAlert = {
  id: string;
  description: string;
  time: string;
  title: string;
};

export type WorkbenchUser = {
  name: string;
  role: string;
};

export type ScheduledConditionStatus = "pending" | "running" | "completed" | "warning" | "error";

export type ScheduledWorkOrderStatus = "pending" | "running" | "awaiting_reply";

export type ScheduledConditionItemStatus = ScheduledConditionStatus | ScheduledWorkOrderStatus;

export type ScheduledConditionRiskLevel = "normal" | "attention" | "critical";

export type ScheduledConditionTaskId = "scada-diagnosis" | "smart-dispatch" | "pump-energy" | "network-simulation";

export type ScheduledConditionSolutionOption = {
  id: string;
  title: string;
  scenario: string;
  action: string;
  tradeoff: string;
};

export type ScheduledConditionAnalysisInsight = {
  summary: string;
  notes: string[];
  escalationCriteria?: string[];
  solutionOptions?: ScheduledConditionSolutionOption[];
};

export type ScheduledConditionKpi = {
  id: string;
  label: string;
  value: string;
  unit?: string;
  baseline?: string;
  threshold?: string;
  status: ScheduledConditionRiskLevel;
  description: string;
};

export type ScheduledConditionReport = {
  title: string;
  conclusion: string;
  sections: {
    title: string;
    items: string[];
  }[];
};

type ScheduledConditionBase<TStatus extends ScheduledConditionItemStatus> = {
  id: string;
  scheduledAt: string;
  title: string;
  summary: string;
  status: TStatus;
  riskLevel: ScheduledConditionRiskLevel;
  updatedAt: number;
  detail?: string;
  recommendation?: string;
  durationMinutes?: number;
};

export type ScheduledConditionRecord = ScheduledConditionBase<ScheduledConditionStatus> & {
  kind: "condition";
  taskId: ScheduledConditionTaskId;
  sessionId: string;
  evidence?: string[];
  modelName?: string;
  analysisInsight?: ScheduledConditionAnalysisInsight;
  kpis?: ScheduledConditionKpi[];
  report?: ScheduledConditionReport;
};

export type ScheduledWorkOrderItem = ScheduledConditionBase<ScheduledWorkOrderStatus> & {
  kind: "work_order";
  code: string;
  source: string;
  location: string;
  dispatcher: string;
  assignee: string;
  priority: "normal" | "urgent";
  replyWindowMinutes: number;
  stages: ScheduledWorkOrderStage[];
  replyRequirements: string[];
  evidence?: never;
  modelName?: never;
  sessionId?: never;
};

export type ScheduledWorkOrderStageId = "dispatch" | "execution" | "reply";

export type ScheduledWorkOrderStageStatus = "done" | "current" | "pending";

export type ScheduledWorkOrderStage = {
  id: ScheduledWorkOrderStageId;
  title: string;
  status: ScheduledWorkOrderStageStatus;
  owner: string;
  timeLabel: string;
  description: string;
};

export type ScheduledConditionItem = ScheduledConditionRecord | ScheduledWorkOrderItem;

export type MapAnnotation = {
  id: string;
  label: string;
  coordinate: [number, number];
  kind: "burst" | "pipe-label" | "district" | "tooltip";
};

export type WorkbenchMap = MapLibreMap;
