import type { Attachment } from '@/lib/attachments';
import { CodeBlock } from '@/components/CodeBlock';
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog';

interface Props {
  attachment: Attachment | null;
  onClose: () => void;
}

export function AttachmentPreviewModal({ attachment, onClose }: Props) {
  const title = attachment
    ? attachment.kind === 'lines' && attachment.range
      ? `${attachment.filename}:${attachment.range[0]}-${attachment.range[1]}`
      : attachment.filename
    : '';

  return (
    <Dialog open={attachment !== null} onOpenChange={() => onClose()}>
      <DialogContent className="sm:max-w-2xl max-h-[80vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="font-mono text-sm">{title}</DialogTitle>
        </DialogHeader>
        {attachment && (
          <CodeBlock
            code={attachment.content}
            lang={attachment.language ?? undefined}
          />
        )}
      </DialogContent>
    </Dialog>
  );
}
