'use client';

import {
  Dialog,
  DialogContent,
  DialogTitle,
  VisuallyHidden,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';
import {
  Edit,
  X as XIcon,
  Printer,
  Send,
} from 'lucide-react';
import { toast } from 'sonner';
import { printArea } from '@/lib/print-utils';
import type { ContractDetail } from '../types';

interface ContractDocumentModalProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  contract: ContractDetail;
}

export function ContractDocumentModal({
  open,
  onOpenChange,
  contract,
}: ContractDocumentModalProps) {
  // 수정
  const handleEdit = () => {
    toast.info('수정 기능은 준비 중입니다.');
  };

  // 상신 (전자결재)
  const handleSubmit = () => {
    toast.info('전자결재 상신 기능은 준비 중입니다.');
  };

  // 인쇄
  const handlePrint = () => {
    printArea({ title: '계약서 인쇄' });
  };

  // PDF URL 확인
  const pdfUrl = contract.contractFile?.fileUrl;

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="max-w-[95vw] md:max-w-[800px] lg:max-w-[900px] h-[95vh] p-0 flex flex-col gap-0 overflow-hidden [&>button]:hidden">
        <VisuallyHidden>
          <DialogTitle>계약서 상세</DialogTitle>
        </VisuallyHidden>

        {/* 헤더 영역 - 고정 (인쇄 시 숨김) */}
        <div className="print-hidden flex-shrink-0 flex items-center justify-between px-6 py-4 border-b bg-white">
          <h2 className="text-lg font-semibold">계약서 상세</h2>
          <Button
            variant="ghost"
            size="icon"
            onClick={() => onOpenChange(false)}
            className="h-8 w-8"
          >
            <XIcon className="h-4 w-4" />
          </Button>
        </div>

        {/* 버튼 영역 - 고정 (인쇄 시 숨김) */}
        <div className="print-hidden flex-shrink-0 flex items-center gap-2 px-6 py-3 bg-muted/30 border-b">
          <Button variant="outline" size="sm" onClick={handleEdit}>
            <Edit className="h-4 w-4 mr-1" />
            수정
          </Button>
          <Button variant="default" size="sm" onClick={handleSubmit} className="bg-blue-600 hover:bg-blue-700">
            <Send className="h-4 w-4 mr-1" />
            상신
          </Button>
          <Button variant="outline" size="sm" onClick={handlePrint}>
            <Printer className="h-4 w-4 mr-1" />
            인쇄
          </Button>
        </div>

        {/* PDF 뷰어 영역 - 스크롤 (인쇄 시 이 영역만 출력) */}
        <div className="print-area flex-1 overflow-y-auto bg-gray-100 p-6">
          <div className="max-w-[210mm] mx-auto bg-white shadow-lg rounded-lg min-h-[297mm]">
            {pdfUrl ? (
              <iframe
                src={pdfUrl}
                className="w-full h-full min-h-[297mm]"
                title="계약서 PDF"
              />
            ) : (
              <div className="flex items-center justify-center h-full min-h-[297mm] text-muted-foreground">
                <p>현재 등록된 계약서가 없습니다.</p>
              </div>
            )}
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
}