import type { AIProviderType } from '@/types/agent-settings'

export interface ChatAttachment {
  id: string
  name: string
  mediaType: string    // 'image/png', 'image/jpeg', etc.
  data: string         // base64-encoded (no data URL prefix)
  size: number         // bytes
}

export interface ChatMessage {
  id: string
  role: 'user' | 'assistant'
  content: string
  timestamp: number
  isStreaming?: boolean
  attachments?: ChatAttachment[]
}

export interface AIDesignRequest {
  prompt: string
  model?: string
  provider?: AIProviderType
  concurrency?: number
  context?: {
    selectedNodes?: string[]
    documentSummary?: string
    canvasSize?: { width: number; height: number }
    variables?: Record<string, import('@/types/variables').VariableDefinition>
    themes?: Record<string, string[]>
  }
}

// ---------------------------------------------------------------------------
// Design System types — generated design tokens for consistency
// ---------------------------------------------------------------------------

export interface DesignSystem {
  palette: {
    background: string
    surface: string
    text: string
    textSecondary: string
    primary: string
    primaryLight: string
    accent: string
    border: string
  }
  typography: {
    headingFont: string
    bodyFont: string
    scale: number[]
  }
  spacing: {
    unit: number
    scale: number[]
  }
  radius: number[]
  aesthetic: string
}

// ---------------------------------------------------------------------------
// Visual Reference types — for the visual reference pipeline
// ---------------------------------------------------------------------------

export interface VisualReference {
  /** Generated HTML/CSS code */
  html: string
  /** Screenshot of the rendered HTML (base64 PNG, no data: prefix) */
  screenshot: string
  /** Design system tokens used */
  designSystem: DesignSystem
  /** Structural summary extracted from the HTML */
  structureSummary: string
}

export interface AICodeRequest {
  prompt?: string
  format: 'react-tailwind' | 'html-css' | 'react-inline'
  nodeIds?: string[]
}

export interface AIStreamChunk {
  type: 'text' | 'thinking' | 'done' | 'error' | 'ping'
  content: string
}

// ---------------------------------------------------------------------------
// Orchestrator types — used for parallel sub-agent design generation
// ---------------------------------------------------------------------------

/** A sub-task produced by the orchestrator (lightweight — only spatial info) */
export interface SubTask {
  /** Unique ID for this sub-task, e.g. "sidebar", "header" */
  id: string
  /** Human-readable label for progress UI */
  label: string
  /** Specific UI elements this subtask must generate (from orchestrator) */
  elements?: string
  /** Spatial region allocated on the canvas */
  region: { width: number; height: number }
  /** ID prefix for all nodes generated by this sub-agent (assigned at runtime) */
  idPrefix: string
  /** Parent frame ID to insert into (assigned at runtime) */
  parentFrameId: string | null
  /** Screen/page grouping — subtasks with the same screen share one root frame */
  screen?: string
  /** HTML reference snippet for this section (from visual reference pipeline) */
  htmlReference?: string
}

/** Style guide produced by the orchestrator for visual consistency */
export interface StyleGuide {
  palette: {
    background: string
    surface: string
    text: string
    secondary: string
    accent: string
    accent2: string
    border: string
  }
  fonts: {
    heading: string
    body: string
  }
  aesthetic: string
}

/** Plan produced by the orchestrator (lightweight — only structure) */
export interface OrchestratorPlan {
  rootFrame: {
    id: string
    name: string
    width: number
    height: number
    layout?: 'none' | 'vertical' | 'horizontal'
    gap?: number
    padding?: number | [number, number] | [number, number, number, number]
    fill?: Array<{ type: string; color: string }>
  }
  styleGuide?: StyleGuide
  subtasks: SubTask[]
}

/** Progress state for the orchestrated generation */
export interface OrchestrationProgress {
  phase: 'planning' | 'generating' | 'merging' | 'done' | 'error'
  subtasks: Array<{
    id: string
    label: string
    status: 'pending' | 'streaming' | 'done' | 'error'
    nodeCount: number
    /** Accumulated thinking content from the sub-agent */
    thinking?: string
    /** Agent identity for visual indicators (concurrent mode only) */
    agentColor?: string
    agentName?: string
  }>
  totalNodes: number
}

/** Result from a single sub-agent */
export interface SubAgentResult {
  subtaskId: string
  nodes: import('@/types/pen').PenNode[]
  rawResponse: string
  error?: string
}
