/**
 * Private Gallery Lightbox Adapter
 *
 * Adapts private gallery media items to the format expected by BundleLightbox
 */

import type { PaidContentPost, PaidContentAttachment } from '../../lib/api'

interface PrivateMediaItem {
  id: number
  storage_id: string
  filename: string
  description?: string | null
  file_hash: string
  file_size: number
  file_type: 'image' | 'video' | 'other'
  mime_type: string
  width?: number | null
  height?: number | null
  duration?: number | null
  person_id?: number | null
  person?: {
    id: number
    name: string
    relationship: {
      id: number
      name: string
      color: string
    }
  } | null
  media_date: string
  thumbnail_url: string
  stream_url: string
  file_url: string
  tags: Array<{ id: number; name: string; slug: string; color: string }>
}

export interface LightboxAdapterResult {
  pseudoPost: PaidContentPost
  attachments: PaidContentAttachment[]
}

/**
 * Adapt private gallery items to BundleLightbox format
 */
export function adaptForLightbox(
  items: PrivateMediaItem[],
  currentIndex: number = 0
): LightboxAdapterResult {
  const currentItem = items[currentIndex]

  // Create a pseudo-post object that BundleLightbox expects
  const pseudoPost: PaidContentPost = {
    id: 0,
    creator_id: 0,
    post_id: 'private-gallery',
    title: currentItem?.person?.name || 'Private Gallery',
    content: currentItem?.description || '',
    published_at: currentItem?.media_date || null,
    added_at: currentItem?.media_date || null,
    has_attachments: items.length > 0 ? 1 : 0,
    attachment_count: items.length,
    downloaded: 1,
    download_date: null,
    is_favorited: 0,
    is_viewed: 1,
    is_pinned: 0,
    pinned_at: null,
    username: 'private',
    platform: 'private-gallery',
    service_id: 'private',
    display_name: currentItem?.person?.name || null,
    profile_image_url: null,
    identity_id: null,
    attachments: [],
    embeds: [],
    tags: (currentItem?.tags || []).map(tag => ({
      ...tag,
      description: null,  // PaidContentTag requires description field
    })),
  }

  // Convert items to attachments format
  const attachments: PaidContentAttachment[] = items.map((item, index) => ({
    id: item.id,
    post_id: 0,
    attachment_index: index,
    name: item.filename,
    file_type: item.file_type === 'video' ? 'video' : item.file_type === 'image' ? 'image' : 'other',
    extension: item.filename.split('.').pop() || '',
    server_path: item.file_url,
    download_url: item.file_url,  // PaidContentAttachment requires download_url field
    file_size: item.file_size,
    file_hash: item.file_hash,
    status: 'completed',
    error_message: null,
    download_attempts: 0,
    local_path: item.file_url,
    local_filename: item.filename,
    downloaded_at: item.media_date,
    created_at: item.media_date,
    width: item.width || null,
    height: item.height || null,
    duration: item.duration || null,
  }))

  return { pseudoPost, attachments }
}

/**
 * Get thumbnail URL for BundleLightbox
 * Since private gallery uses different endpoint, we need to map it
 */
export function getThumbnailUrl(item: PrivateMediaItem): string {
  // The thumbnail_url already includes the full path
  return item.thumbnail_url
}

/**
 * Get file URL for BundleLightbox
 */
export function getFileUrl(item: PrivateMediaItem): string {
  return item.file_type === 'video' ? item.stream_url : item.file_url
}

export default adaptForLightbox
