/**
 * VideoErrorFallback - Pre-built error fallback with download button
 * For use with VideoPlayer errorFallback prop
 */

'use client';

import React from 'react';
import { FileVideo, RefreshCw } from 'lucide-react';

import { cn, Button, DownloadButton } from '../../_shared';

import type { ErrorFallbackProps } from '../types';

// ============================================================================
// Types
// ============================================================================

export interface VideoErrorFallbackProps extends ErrorFallbackProps {
  /** URL for download button (if provided, shows download button) */
  downloadUrl?: string;
  /** Filename for download */
  downloadFilename?: string;
  /** File size to display */
  fileSize?: string;
  /** Show retry button */
  showRetry?: boolean;
  /** Custom className */
  className?: string;
  /** Custom icon */
  icon?: React.ReactNode;
  /** Custom title (defaults to error message) */
  title?: string;
  /** Custom description */
  description?: string;
}

// ============================================================================
// Component
// ============================================================================

/**
 * Pre-built error fallback component for VideoPlayer
 *
 * @example
 * // Basic usage
 * <VideoPlayer
 *   source={source}
 *   errorFallback={(props) => (
 *     <VideoErrorFallback
 *       {...props}
 *       downloadUrl={getDownloadUrl()}
 *       downloadFilename="video.mp4"
 *     />
 *   )}
 * />
 *
 * @example
 * // With file size
 * <VideoErrorFallback
 *   error="Failed to load video"
 *   downloadUrl="/api/download/video.mp4"
 *   fileSize="125 MB"
 *   showRetry
 *   retry={() => reloadVideo()}
 * />
 */
export function VideoErrorFallback({
  error,
  retry,
  downloadUrl,
  downloadFilename,
  fileSize,
  showRetry = true,
  className,
  icon,
  title,
  description,
}: VideoErrorFallbackProps) {
  const displayTitle = title || error || 'Video cannot be previewed';

  return (
    <div
      className={cn(
        'absolute inset-0 flex flex-col items-center justify-center gap-4 bg-black/90 text-white p-6',
        className
      )}
    >
      {/* Icon */}
      {icon || <FileVideo className="w-16 h-16 text-muted-foreground" />}

      {/* Title */}
      <p className="text-lg font-medium text-center">{displayTitle}</p>

      {/* Description / File size */}
      {(description || fileSize) && (
        <p className="text-sm text-muted-foreground text-center">
          {description || fileSize}
        </p>
      )}

      {/* Actions */}
      <div className="flex items-center gap-3 mt-2">
        {/* Retry button */}
        {showRetry && retry && (
          <Button
            variant="outline"
            size="sm"
            onClick={retry}
            className="gap-2"
          >
            <RefreshCw className="w-4 h-4" />
            Retry
          </Button>
        )}

        {/* Download button */}
        {downloadUrl && (
          <DownloadButton
            url={downloadUrl}
            filename={downloadFilename}
            variant="default"
            size="sm"
          >
            Download to view
          </DownloadButton>
        )}
      </div>
    </div>
  );
}

// ============================================================================
// Factory for common use cases
// ============================================================================

export interface CreateVideoErrorFallbackOptions {
  /** Function to get download URL from source */
  getDownloadUrl?: (source: unknown) => string | undefined;
  /** Function to get filename from source */
  getFilename?: (source: unknown) => string | undefined;
  /** Function to get file size from source */
  getFileSize?: (source: unknown) => string | undefined;
  /** Show retry button */
  showRetry?: boolean;
}

/**
 * Factory to create error fallback function for VideoPlayer
 *
 * @example
 * const errorFallback = createVideoErrorFallback({
 *   getDownloadUrl: (source) => source.downloadUrl,
 *   getFilename: (source) => source.filename,
 *   showRetry: true,
 * });
 *
 * <VideoPlayer source={source} errorFallback={errorFallback} />
 */
export function createVideoErrorFallback(
  options: CreateVideoErrorFallbackOptions
): (props: ErrorFallbackProps, source?: unknown) => React.ReactNode {
  return (props: ErrorFallbackProps, source?: unknown) => (
    <VideoErrorFallback
      {...props}
      downloadUrl={options.getDownloadUrl?.(source)}
      downloadFilename={options.getFilename?.(source)}
      fileSize={options.getFileSize?.(source)}
      showRetry={options.showRetry}
    />
  );
}
