import { ReactNode, ComponentType } from 'react';

// ============================================================
// Action Types
// ============================================================

export type ActionType = 'print' | 'download' | 'edit' | 'delete' | 'submit' | 'approve' | 'reject' | 'copy' | 'pdf' | 'email' | 'fax' | 'kakao';

export interface ActionHandlers {
  onPrint?: () => void;
  onDownload?: () => void;
  onEdit?: () => void;
  onDelete?: () => void;
  onSubmit?: () => void;
  onApprove?: () => void;
  onReject?: () => void;
  onCopy?: () => void;
  onPdf?: () => void;
  onEmail?: () => void;
  onFax?: () => void;
  onKakao?: () => void;
}

// ============================================================
// Feature Types
// ============================================================

export interface DocumentFeatures {
  zoom?: boolean;
  drag?: boolean;
  print?: boolean;
  download?: boolean;
}

// ============================================================
// Preset Types
// ============================================================

export type PresetType = 'inspection' | 'construction' | 'approval' | 'approval-draft' | 'approval-inbox' | 'readonly' | 'quote';

export interface PresetConfig {
  features: DocumentFeatures;
  actions: ActionType[];
}

// ============================================================
// Document Config
// ============================================================

export interface DocumentConfig {
  // 메타 정보
  type: string;
  title: string;
  preset?: PresetType;

  // 뷰어 설정 (프리셋 오버라이드)
  features?: DocumentFeatures;
  actions?: ActionType[];

  // 콘텐츠 설정 - Phase 1: 정적 모드
  component?: ComponentType<any>;

  // 콘텐츠 설정 - Phase 2: 동적 모드 (빌더용)
  blocks?: DocumentBlock[];
}

// ============================================================
// Block Types (문서 빌더용)
// ============================================================

export type DocumentBlock =
  | HeaderBlock
  | InfoTableBlock
  | ItemTableBlock
  | ApprovalLineBlock
  | SignatureBlock
  | TextSectionBlock
  | ImageGridBlock
  | CustomBlock;

export interface HeaderBlock {
  type: 'header';
  title: string;
  logo?: boolean;
  companyName?: string;
  showApprovalLine?: boolean;
  approvalPositions?: string[];
}

export interface InfoTableBlock {
  type: 'info-table';
  columns: 2 | 3 | 4;
  fields: {
    label: string;
    key: string;
    colSpan?: number;
    rowSpan?: number;
  }[];
}

export interface ItemTableBlock {
  type: 'item-table';
  columns: {
    key: string;
    label: string;
    width?: string;
    align?: 'left' | 'center' | 'right';
  }[];
  showTotal?: boolean;
  totalFields?: string[];
}

export interface ApprovalLineBlock {
  type: 'approval-line';
  positions: string[];
  layout?: 'horizontal' | 'vertical';
}

export interface SignatureBlock {
  type: 'signature';
  positions: { label: string; name?: string }[];
}

export interface TextSectionBlock {
  type: 'text-section';
  title?: string;
  content: string;
  style?: 'normal' | 'highlight' | 'note';
}

export interface ImageGridBlock {
  type: 'image-grid';
  columns: 2 | 3 | 4;
  imageKey: string; // data에서 이미지 배열을 가져올 키
}

export interface CustomBlock {
  type: 'custom';
  componentKey: string;
  props?: Record<string, any>;
}

// ============================================================
// DocumentViewer Props
// ============================================================

// ============================================================
// PDF Meta Types
// ============================================================

export interface PdfMeta {
  documentNumber?: string;
  createdDate?: string;
  showHeaderFooter?: boolean;
}

// ============================================================
// DocumentViewer Props
// ============================================================

export interface DocumentViewerProps {
  // Config 기반 (권장)
  config?: DocumentConfig;

  // 개별 props (하위 호환)
  title?: string;
  subtitle?: string;
  preset?: PresetType;
  features?: DocumentFeatures;
  actions?: ActionType[];

  // 데이터
  data?: any;

  // PDF 메타 정보 (헤더/푸터용)
  pdfMeta?: PdfMeta;

  // 액션 핸들러
  onPrint?: () => void;
  onDownload?: () => void;
  onEdit?: () => void;
  onDelete?: () => void;
  onSubmit?: () => void;
  onApprove?: () => void;
  onReject?: () => void;
  onCopy?: () => void;
  onPdf?: () => void;
  onEmail?: () => void;
  onFax?: () => void;
  onKakao?: () => void;

  // 모달 제어
  open?: boolean;
  onOpenChange?: (open: boolean) => void;

  // 정적 모드용 children
  children?: ReactNode;

  // 툴바 확장 영역 (액션 버튼 옆에 추가 요소)
  toolbarExtra?: ReactNode;
}

// ============================================================
// Hook Types
// ============================================================

export interface UseZoomOptions {
  defaultZoom?: number;
  minZoom?: number;
  maxZoom?: number;
  zoomLevels?: number[];
}

export interface UseZoomReturn {
  zoom: number;
  zoomIn: () => void;
  zoomOut: () => void;
  zoomReset: () => void;
  setZoom: (zoom: number) => void;
  canZoomIn: boolean;
  canZoomOut: boolean;
}

export interface UseDragOptions {
  enabled?: boolean;
}

export interface UseDragReturn {
  position: { x: number; y: number };
  isDragging: boolean;
  handlers: {
    onMouseDown: (e: React.MouseEvent) => void;
    onMouseMove: (e: React.MouseEvent) => void;
    onMouseUp: () => void;
    onMouseLeave: () => void;
    onTouchStart: (e: React.TouchEvent) => void;
    onTouchMove: (e: React.TouchEvent) => void;
    onTouchEnd: () => void;
  };
  resetPosition: () => void;
}
