import type { CreatePreviewTaskInput } from "../../api/webGenerationGateway";
import type {
  WebCanvasWorkflow,
  WebCanvasWorkflowAssetRef,
  WebCanvasWorkflowTaskRef,
  WebGenerationPreviewTask,
  WebProjectSummary,
  WebUserSession,
} from "../../types";

export type { CreatePreviewTaskInput, WebCanvasWorkflow, WebCanvasWorkflowAssetRef, WebCanvasWorkflowTaskRef, WebGenerationPreviewTask, WebProjectSummary, WebUserSession };

export interface CanvasSaveWorkflowOptions {
  silent?: boolean;
  reason?: "manual" | "autosave" | "publish";
}

export interface CanvasPageProps {
  workflow: WebCanvasWorkflow;
  projectId?: string | null;
  projects?: WebProjectSummary[];
  projectsLoaded?: boolean;
  onOpenCommunity: () => void;
  onOpenProject?: (project: WebProjectSummary) => void;
  onStartCreate?: () => void;
  isAuthenticated: boolean;
  session: WebUserSession | null;
  onOpenLogin: () => void;
  onSaveWorkflow?: (workflow: WebCanvasWorkflow, options?: CanvasSaveWorkflowOptions) => Promise<WebProjectSummary | void>;
  onCreateTask?: (input: CreatePreviewTaskInput) => Promise<WebGenerationPreviewTask>;
}

export interface CanvasTextNode {
  id: string;
  title: string;
  prompt: string;
  content: string;
  isEditingContent: boolean;
  isComposerOpen: boolean;
  selectedModelId: string;
  position: {
    x: number;
    y: number;
  };
  size: CanvasNodeSize;
}

export interface CanvasTextNodeDrag {
  nodeId: string;
  startX: number;
  startY: number;
  originX: number;
  originY: number;
  hasMoved: boolean;
}

export interface CanvasTextGenerationState {
  status: "running" | "success" | "error";
  message: string;
}

export interface CanvasImageNode {
  id: string;
  title: string;
  prompt: string;
  imageUrl: string;
  model: string;
  aspectRatio: string;
  imageSize: string;
  fileName?: string;
  assetRef?: WebCanvasWorkflowAssetRef | null;
  taskRef?: WebCanvasWorkflowTaskRef | null;
  styleReference?: CanvasStyleReference;
  focusSelection?: CanvasImageFocusSelection;
  marking?: string;
  sourceTextNodeId?: string;
  sourceImageNodeId?: string;
  position: {
    x: number;
    y: number;
  };
  size: CanvasNodeSize;
}

export interface CanvasImageReferenceItem {
  nodeId: string;
  title: string;
  imageUrl: string;
  fileName?: string;
}

export interface CanvasImageNodeDrag {
  nodeId: string;
  startX: number;
  startY: number;
  originX: number;
  originY: number;
  hasMoved: boolean;
}

export interface CanvasImageGenerationState {
  status: "submitting" | "running" | "success" | "error";
  message: string;
  progress?: number;
}

export interface CanvasImageFocusSelection {
  x: number;
  y: number;
  width: number;
  height: number;
  ratio: string;
}

export interface CanvasImageFocusDrag {
  nodeId: string;
  startX: number;
  startY: number;
  rect: {
    left: number;
    top: number;
    width: number;
    height: number;
  };
}

export interface CanvasStyleReference {
  id: string;
  title: string;
  author: string;
  category: string;
  imageUrl: string;
  prompt?: string;
}

export interface CanvasStyleCase extends CanvasStyleReference {
  summary: string;
  favoriteCount: number;
  likeCount: number;
  isFavorited: boolean;
  keywords: string;
}

export type CanvasStylePickerTab = "square" | "favorites" | "recent";

export interface CanvasVideoNode {
  id: string;
  title: string;
  prompt: string;
  videoUrl?: string;
  model: string;
  aspectRatio: string;
  resolution: string;
  duration: string;
  videoMode: "text2video" | "img2video" | "firstlast";
  assetRef?: WebCanvasWorkflowAssetRef | null;
  taskRef?: WebCanvasWorkflowTaskRef | null;
  marking?: string;
  cameraMotion?: string;
  sourceTextNodeId: string;
  position: {
    x: number;
    y: number;
  };
  size: CanvasNodeSize;
}

export type CanvasVideoMode = CanvasVideoNode["videoMode"];

export interface CanvasVideoNodeDrag {
  nodeId: string;
  startX: number;
  startY: number;
  originX: number;
  originY: number;
  hasMoved: boolean;
}

export interface CanvasVideoGenerationState {
  status: "submitting" | "running" | "success" | "error";
  message: string;
  progress?: number;
}

export type CanvasNodeKind = "text" | "image" | "video";
export type CanvasNodeSide = "left" | "right";
export type CanvasNodePortSlot = "center";

export type CanvasCopiedNode =
  | { kind: "text"; node: CanvasTextNode }
  | { kind: "image"; node: CanvasImageNode }
  | { kind: "video"; node: CanvasVideoNode };

export interface CanvasNodeSize {
  width: number;
  height: number;
}

export interface CanvasSelectedNode {
  kind: CanvasNodeKind;
  id: string;
}

export interface CanvasNodePackage {
  id: string;
  title: string;
  nodeIds: CanvasSelectedNode[];
  collapsed?: boolean;
  collapsedBounds?: CanvasNodeBounds;
  updatedAt?: string;
}

export interface CanvasNodeBounds {
  left: number;
  top: number;
  width: number;
  height: number;
}

export interface CanvasNodePackageDrag {
  packageId: string;
  collapsed: boolean;
  startX: number;
  startY: number;
  hasMoved: boolean;
  textOrigins: Record<string, CanvasPoint>;
  imageOrigins: Record<string, CanvasPoint>;
  videoOrigins: Record<string, CanvasPoint>;
  collapsedBounds?: CanvasNodeBounds;
}

export interface CanvasNodeResizeDrag {
  kind: CanvasNodeKind;
  nodeId: string;
  startX: number;
  startY: number;
  originWidth: number;
  originHeight: number;
}

export interface CanvasSelectionDrag {
  start: CanvasPoint;
  current: CanvasPoint;
  hasMoved: boolean;
}

export interface CanvasViewport {
  x: number;
  y: number;
  zoom: number;
}

export interface CanvasPanDrag {
  startX: number;
  startY: number;
  originX: number;
  originY: number;
}

export interface CanvasAlignGuide {
  axis: "x" | "y";
  position: number;
}

export interface CanvasNodePort {
  nodeId: string;
  kind: CanvasNodeKind;
  side: CanvasNodeSide;
  slot: CanvasNodePortSlot;
}

export interface CanvasManualLink {
  id: string;
  from: CanvasNodePort;
  to: CanvasNodePort;
}

// --- Canvas @-mention types ---

export interface CanvasPromptMentionOption {
  token: string;       // e.g. "@图片1"
  label: string;       // e.g. "图片1"
  kind: "image" | "video" | "text";
  nodeId: string;
  nodeTitle: string;
  previewUrl?: string;
  searchText: string;
}

export interface CanvasPromptMentionState {
  open: boolean;
  query: string;
  start: number;
  caret: number;
  activeIndex: number;
}

export interface CanvasAssetSaveSource {
  kind: "text" | "image" | "video";
  name: string;
  description: string;
  url?: string;
  imageUrl?: string;
}

export interface CanvasProjectSaveState {
  status: "idle" | "saving" | "success" | "error";
  message: string;
}

export interface CanvasFloatingMenuPosition {
  left: number;
  top: number;
  originLeft: number;
  originTop: number;
}

export interface CanvasConnectorFollowOffset {
  x: number;
  y: number;
}

export interface CanvasConnectorDrag {
  port: CanvasNodePort;
  startX: number;
  startY: number;
  hasMoved: boolean;
}

export interface CanvasPoint {
  x: number;
  y: number;
}

export interface CanvasOption {
  value: string;
  label: string;
}
