/**
 * Player configuration types
 */

import type React from 'react';
import type { VideoSourceUnion } from './sources';

// =============================================================================
// Player Mode
// =============================================================================

/** Player mode - determines which provider to use */
export type PlayerMode =
  | 'auto'      // Auto-select based on source type
  | 'vidstack'  // Force Vidstack (full-featured)
  | 'native'    // Force native HTML5 <video>
  | 'streaming'; // Force streaming provider

// =============================================================================
// Player Props
// =============================================================================

/** Aspect ratio options */
export type AspectRatioValue = number | 'auto' | 'fill';

/** Common player settings */
export interface CommonPlayerSettings {
  /** Auto-play video */
  autoPlay?: boolean;
  /** Mute video by default */
  muted?: boolean;
  /** Loop video */
  loop?: boolean;
  /** Play video inline on mobile */
  playsInline?: boolean;
  /** Show player controls */
  controls?: boolean;
  /**
   * Aspect ratio:
   * - number (e.g. 16/9): Fixed aspect ratio
   * - 'auto': Natural video aspect ratio
   * - 'fill': Fill parent container (100% width & height)
   */
  aspectRatio?: AspectRatioValue;
  /** Preload strategy */
  preload?: 'auto' | 'metadata' | 'none';
}

/** Common player events */
export interface CommonPlayerEvents {
  onPlay?: () => void;
  onPause?: () => void;
  onEnded?: () => void;
  onError?: (error: string) => void;
  onLoadStart?: () => void;
  onCanPlay?: () => void;
  onTimeUpdate?: (currentTime: number, duration: number) => void;
  /** Called when buffering progress changes (buffered seconds, total duration) */
  onBufferProgress?: (buffered: number, duration: number) => void;
}

/** Error fallback render props */
export interface ErrorFallbackProps {
  error: string;
  retry?: () => void;
}

/** Main VideoPlayer props */
export interface VideoPlayerProps extends CommonPlayerSettings, CommonPlayerEvents {
  /** Video source configuration */
  source: VideoSourceUnion;
  /** Player mode (default: 'auto') */
  mode?: PlayerMode;
  /** Player theme (Vidstack only) */
  theme?: 'default' | 'minimal' | 'modern';
  /** Show video info below player */
  showInfo?: boolean;
  /** Container className */
  className?: string;
  /** Video element className (native/streaming only) */
  videoClassName?: string;
  /** Disable right-click context menu */
  disableContextMenu?: boolean;
  /** Show loading spinner */
  showPreloader?: boolean;
  /** Preloader timeout in ms */
  preloaderTimeout?: number;
  /** Custom error fallback UI */
  errorFallback?: React.ReactNode | ((props: ErrorFallbackProps) => React.ReactNode);
}

/** VideoPlayer ref methods */
export interface VideoPlayerRef {
  /** Play video */
  play: () => Promise<void> | void;
  /** Pause video */
  pause: () => void;
  /** Toggle play/pause */
  togglePlay: () => void;
  /** Seek to time in seconds */
  seekTo: (time: number) => void;
  /** Set volume (0-1) */
  setVolume: (volume: number) => void;
  /** Toggle mute */
  toggleMute: () => void;
  /** Enter fullscreen */
  enterFullscreen: () => void;
  /** Exit fullscreen */
  exitFullscreen: () => void;
  /** Current playback time in seconds */
  readonly currentTime: number;
  /** Video duration in seconds */
  readonly duration: number;
  /** Whether video is paused */
  readonly paused: boolean;
}
