'use client';

import * as React from 'react';
import { AlertTriangle, Info, X } from 'lucide-react';
import { Modal } from '@/components/ui/Modal';
import { Button } from '@/components/ui/Button';
import { formatMemorySize } from '@/lib/utils/memory-limits';

interface MemoryWarningDialogProps {
  open: boolean;
  estimatedMemoryMB: number;
  availableMemoryMB?: number;
  warning: string;
  fileName?: string;
  onContinue: () => void;
  onCancel: () => void;
}

export function MemoryWarningDialog({
  open,
  estimatedMemoryMB,
  availableMemoryMB,
  warning,
  fileName,
  onContinue,
  onCancel,
}: MemoryWarningDialogProps) {
  if (!open) return null;

  const estimatedBytes = estimatedMemoryMB * 1024 * 1024;
  const availableBytes = availableMemoryMB ? availableMemoryMB * 1024 * 1024 : undefined;

  return (
    <Modal open={open} onClose={onCancel} title="">
      <div className="p-6 max-w-md">
        {/* Header */}
        <div className="flex items-start justify-between mb-4">
          <div className="flex items-center gap-2">
            <AlertTriangle className="h-5 w-5 text-yellow-500" />
            <h2 className="text-lg font-semibold">Memory Warning</h2>
          </div>
          <button onClick={onCancel} className="text-muted-foreground hover:text-foreground">
            <X className="h-4 w-4" />
          </button>
        </div>

        <p className="text-sm text-muted-foreground mb-4">
          {warning}
        </p>

        <div className="space-y-4">
          {/* File Info */}
          {fileName && (
            <div className="flex items-center gap-2 text-sm">
              <Info className="h-4 w-4 text-muted-foreground" />
              <span className="font-medium">File:</span>
              <span className="text-muted-foreground truncate">{fileName}</span>
            </div>
          )}

          {/* Memory Details */}
          <div className="bg-muted/50 border border-border rounded-md p-3 space-y-2">
            <div className="flex justify-between text-sm">
              <span className="text-muted-foreground">Estimated Memory:</span>
              <span className="font-medium">{formatMemorySize(estimatedBytes)}</span>
            </div>
            {availableBytes && (
              <div className="flex justify-between text-sm">
                <span className="text-muted-foreground">Available Memory:</span>
                <span className="font-medium">{formatMemorySize(availableBytes)}</span>
              </div>
            )}
          </div>

          {/* Warning Message */}
          <div className="bg-yellow-500/10 border border-yellow-500/20 rounded-md p-3">
            <p className="text-sm text-yellow-700 dark:text-yellow-400">
              <strong>Note:</strong> Loading large files may cause performance issues or browser crashes,
              especially on devices with limited memory. Consider:
            </p>
            <ul className="mt-2 text-sm text-yellow-700 dark:text-yellow-400 space-y-1 list-disc list-inside">
              <li>Closing other browser tabs</li>
              <li>Using a shorter audio file</li>
              <li>Splitting large files into smaller segments</li>
            </ul>
          </div>

          {/* Actions */}
          <div className="flex justify-end gap-2">
            <Button onClick={onCancel} variant="outline">
              Cancel
            </Button>
            <Button onClick={onContinue} variant="default">
              Continue Anyway
            </Button>
          </div>
        </div>
      </div>
    </Modal>
  );
}
