import { photos as photosApi } from '../../services/api'
import type { Photo } from '../../types/photo'

const VIDEO_EXTENSIONS = ['.mp4', '.mov', '.webm', '.mkv', '.m4v']

export function isVideo(photo: Photo): boolean {
  if (photo.media_type === 'video') return true
  const lower = photo.filepath.toLowerCase()
  return VIDEO_EXTENSIONS.some((ext) => lower.endsWith(ext))
}

/**
 * Pick the best display URL for a still photo in the preview view.
 *
 * Always uses the /proxy endpoint, which the backend resolves to:
 *   - the original file for web-safe formats (JPEG/PNG/WebP/GIF)
 *   - a transcoded full-res WebP for RAW/HEIC/TIFF (cached on first hit)
 *
 * Videos go through `getVideoSrc` instead and use /original directly.
 */
export function getPreviewImageSrc(photo: Photo): string {
  return photosApi.getProxyUrl(photo.id)
}

/** Fallback used when the proxy endpoint fails or 404s — shows the 1280px
 *  large thumbnail so the user still sees something. */
export function getPreviewFallbackSrc(photo: Photo): string {
  return photosApi.getThumbnailUrl(photo.id, 'large')
}

export function getVideoSrc(photo: Photo): string {
  return photosApi.getOriginalUrl(photo.id)
}
