/** Canvas object types, artboard configuration, and canvas state. */

import type { TraceMetadata } from './engine';

// -- Artboard --

export type ArtboardShape =
  | 'rect'
  | 'square'
  | 'circle'
  | 'oval'
  | 'shield'
  | 'pennant'
  | 'custom';

export type ArtboardUnit = 'inches' | 'mm';

export interface ArtboardConfig {
  shape: ArtboardShape;
  width: number;         // in current unit
  height: number;        // in current unit
  unit: ArtboardUnit;
  /** Custom clip path data (SVG path "d" attr), used only for shield/pennant/custom. */
  clipPath?: string;
}

// -- Canvas Objects (discriminated union) --

interface BaseCanvasObject {
  id: string;
  name: string;
  x: number;
  y: number;
  rotation: number;
  visible: boolean;
  locked: boolean;
  opacity: number;
}

export interface RectObject extends BaseCanvasObject {
  type: 'rect';
  width: number;
  height: number;
  fill: string;
  stroke: string;
  strokeWidth: number;
}

export interface CircleObject extends BaseCanvasObject {
  type: 'circle';
  radius: number;
  fill: string;
  stroke: string;
  strokeWidth: number;
}

export interface EllipseObject extends BaseCanvasObject {
  type: 'ellipse';
  radiusX: number;
  radiusY: number;
  fill: string;
  stroke: string;
  strokeWidth: number;
}

export type LineStyle = 'solid' | 'dashed' | 'dotted';

export interface LineObject extends BaseCanvasObject {
  type: 'line';
  points: number[];    // [x1, y1, x2, y2, ...]
  stroke: string;
  strokeWidth: number;
  lineStyle: LineStyle;
  dash: number[];
}

export interface ImageObject extends BaseCanvasObject {
  type: 'image';
  width: number;
  height: number;
  /** Blob URL or data URL for the image source. */
  src: string;
}

export interface TextObject extends BaseCanvasObject {
  type: 'text';
  text: string;
  fontFamily: string;
  fontSize: number;
  letterSpacing: number;
  lineHeight: number;
  fill: string;
  stroke: string;
  strokeWidth: number;
  /** Wrapping width for text layout. */
  width: number;
}

export type CanvasObject =
  | RectObject
  | CircleObject
  | EllipseObject
  | LineObject
  | ImageObject
  | TextObject;

export type CanvasObjectType = CanvasObject['type'];

// -- Canvas State --

export interface CanvasState {
  objects: CanvasObject[];
  selectedIds: string[];
  artboard: ArtboardConfig | null;
  traceMetadata: TraceMetadata | null;
}
