/**
 * Migration Script Types
 * Story 1.3: Content Migration Script
 */

import { File } from 'payload'

// Lexical content type (simplified)
export interface LexicalRoot {
  type: 'root'
  version: 1
  children: Array<any>
  direction: string | null
}

// ============================================================
// WEBFLOW DATA TYPES (from HTML/JSON export)
// ============================================================

export interface WebflowPost {
  title: string
  slug: string
  content: string // HTML content
  publishedDate: string | Date
  postCategory?: string // Category name or slug
  featuredImage?: string // Image URL
  seoTitle?: string
  seoDescription?: string
  excerpt?: string
}

export interface WebflowCategory {
  name: string // Chinese name
  slug: string
  colorHex?: string // Single color to split into text + background
}

export interface WebflowPortfolioItem {
  name: string
  slug: string
  websiteLink: string
  previewImage: string // Image URL
  description: string
  websiteType: 'corporate' | 'ecommerce' | 'landing' | 'brand' | 'other'
  tags: string // Comma-separated string
}

export interface WebflowExportData {
  posts?: WebflowPost[]
  categories?: WebflowCategory[]
  portfolio?: WebflowPortfolioItem[]
}

// ============================================================
// PAYLOAD CMS DATA TYPES
// ============================================================

export interface PayloadCategory {
  title: string
  nameEn?: string
  order: number
  textColor: string
  backgroundColor: string
  slug: string
}

export interface PayloadPostData {
  title: string
  slug: string
  heroImage?: string // Media ID
  ogImage?: string // Media ID
  content: string // Lexical JSON string for richText field
  excerpt?: string
  publishedAt: Date
  status: 'draft' | 'review' | 'published'
  categories?: string[] // Category IDs
  meta?: {
    title?: string
    description?: string
    image?: string // Media ID
  }
}

export interface PayloadPortfolioData {
  title: string
  slug: string
  url: string
  image?: string // Media ID
  description?: string
  websiteType: 'corporate' | 'ecommerce' | 'landing' | 'brand' | 'other'
  tags?: Array<{ tag: string }>
}

// ============================================================
// MIGRATION RESULT TYPES
// ============================================================

export interface MigrationResult {
  success: boolean
  id?: string
  slug?: string
  error?: string
  skipped?: boolean
}

export interface CollectionMigrationResult {
  collection: string
  created: number
  skipped: number
  failed: number
  results: Array<{
    slug: string
    success: boolean
    id?: string
    error?: string
  }>
}

export interface MigrationReport {
  timestamp: string
  dryRun: boolean
  summary: {
    total: number
    created: number
    skipped: number
    failed: number
  }
  byCollection: {
    categories?: { created: number; skipped: number; failed: number }
    posts?: { created: number; skipped: number; failed: number }
    portfolio?: { created: number; skipped: number; failed: number }
  }
  details: {
    categories?: CollectionMigrationResult
    posts?: CollectionMigrationResult
    portfolio?: CollectionMigrationResult
  }
}

// ============================================================
// MIGRATION CONFIG TYPES
// ============================================================

export interface MigrationConfig {
  dryRun: boolean
  verbose: boolean
  collections: Array<'categories' | 'posts' | 'portfolio' | 'all'>
  force: boolean // Overwrite existing items
  batchSize: number // For batch processing
  sourcePath: string // Path to HTML or JSON file
}

export interface MediaDownloadResult {
  success: boolean
  url: string
  buffer?: Buffer
  filename?: string
  error?: string
}
