import type { LucideIcon } from "lucide-react";
import type React from "react";

export type ArtifactStatus =
  | "draft"
  | "waiting_confirmation"
  | "running"
  | "success"
  | "failed"
  | "paused";

export type ArtifactType =
  | "parameter_change"
  | "device_action"
  | "knowledge_lesson"
  | "guided_procedure"
  | "die_material_adjust"
  | "die_status_overview";


export type ArtifactAction = {
  actionId: string;
  label: string;
  kind: "primary" | "secondary" | "danger" | "input" | "toggle" | "choice";
  textAliases: string[];
  voiceAliases?: string[];
  requiresConfirmation?: boolean;
  disabled?: boolean;
};

export type ArtifactLLMContext = {
  visibleSummary: string;
  currentState: string;
  activeStep?: string;
  visibleFields: Array<{
    label: string;
    value: string;
  }>;
  availableActions: Array<{
    actionId: string;
    label: string;
    textAliases: string[];
    voiceAliases?: string[];
  }>;
  lastEvents: string[];
};

export type InteractionArtifactBase<TType extends ArtifactType, TProps> = {
  artifactId: string;
  type: TType;
  title: string;
  status: ArtifactStatus;
  props: TProps;
  actions: ArtifactAction[];
  llmContext: ArtifactLLMContext;
  snapshotPolicy: "ephemeral" | "session" | "persistent";
};

export type ParameterChangeProps = {
  parameterName: string;
  currentValue: string | number;
  targetValue: string | number;
  unit?: string;
  deviceName: string;
  impact?: string;
  executionNote?: string;
};

export type DeviceActionProps = {
  deviceName: string;
  action: "open" | "close" | "start" | "stop";
  deviceKind: "light" | "pump" | "fan" | "machine";
  currentState: string;
  targetState: string;
  progressLabel?: string;
  executionNote?: string;
};

export type MediaRef = {
  type: "image" | "video";
  title: string;
  url?: string;
  description?: string;
};

export type KnowledgeBlock =
  | { type: "summary"; text: string }
  | { type: "key_points"; items: string[] }
  | {
      type: "steps";
      steps: Array<{
        title: string;
        body: string;
        media?: MediaRef[];
      }>;
    }
  | { type: "media"; media: MediaRef[] }
  | { type: "details"; title: string; markdown: string; defaultOpen?: boolean }
  | { type: "citations"; citations: Array<{ title: string; location: string }> };

export type KnowledgeLessonProps = {
  topic: string;
  conclusion: string;
  blocks: KnowledgeBlock[];
};

export type ProcedureStep = {
  stepId: string;
  type: "auto_tool" | "manual_action" | "measurement_input" | "decision_branch" | "checklist_gate";
  title: string;
  instruction: string;
  status: "pending" | "running" | "waiting_feedback" | "completed" | "failed" | "skipped";
  expectedFeedback?: string[];
  value?: string;
};

export type GuidedProcedureProps = {
  procedureName: string;
  deviceName: string;
  currentStepId: string;
  steps: ProcedureStep[];
};

// ── 模具伺服位置数据 ─────────────────────────────────────────────────────────

export type DiePositionData = {
  dieIndex: number;        // 1-based, 1~16
  label: string;           // 如 "模具 1"
  xPos: number;            // X 轴伺服位置 (mm)
  yPos: number;            // Y 轴伺服位置 (mm)
  angleOffset: number;     // 角度偏移 (°)
  standard: { xPos: number; yPos: number; angleOffset: number }; // 标准值
  deviation: number;       // 综合偏差 (mm)
  status: "normal" | "warning" | "over_limit";
  // 管材横截面形态描述（用于渲染 SVG）
  crossSection: {
    outerRadius: number;   // 外径 (mm)
    wallThickness: number; // 壁厚 (mm)
    ovality: number;       // 椭圆度 0~1（0=正圆）
    ovalityAngle: number;  // 椭圆长轴方向 (°)
  };
};

// ── 材料 + 模具伺服调节 Artifact ─────────────────────────────────────────────

export type DieMaterialAdjustProps = {
  material: string;         // 如 "304 不锈钢"
  thickness: number;        // 壁厚 (mm)
  outerDiameter: number;    // 外径 (mm)
  pipeShape: "round" | "square" | "oval";  // 管型
  totalDies: number;        // 模具数量（默认 15）
  dies: DiePositionData[];  // 各模具位置数据
  executionNote?: string;
};

// ── 模具位置总览 Artifact ─────────────────────────────────────────────────────

export type DieStatusOverviewProps = {
  totalDies: number;         // 16
  selectedDieIndex: number | null; // 当前选中的模具索引
  dies: DiePositionData[];
  lastScanTime?: string;
};


export type InteractionArtifact =
  | InteractionArtifactBase<"parameter_change", ParameterChangeProps>
  | InteractionArtifactBase<"device_action", DeviceActionProps>
  | InteractionArtifactBase<"knowledge_lesson", KnowledgeLessonProps>
  | InteractionArtifactBase<"guided_procedure", GuidedProcedureProps>
  | InteractionArtifactBase<"die_material_adjust", DieMaterialAdjustProps>
  | InteractionArtifactBase<"die_status_overview", DieStatusOverviewProps>;


export type ArtifactActionEvent = {
  artifactId: string;
  actionId: string;
  source: "click" | "text" | "voice" | "system";
  value?: unknown;
  transcript?: string;
  confidence?: number;
  createdAt: string;
};

export type ArtifactComponentProps<T extends InteractionArtifact = InteractionArtifact> = {
  artifact: T;
  onAction: (action: ArtifactAction, value?: unknown) => void;
};

export type ArtifactShellProps = {
  icon: LucideIcon;
  eyebrow: string;
  artifact: InteractionArtifact;
  children: React.ReactNode;
};
