'use client';

import { DocumentViewer } from '@/components/document-system';
import { toast } from 'sonner';
import { useRouter } from 'next/navigation';
import type { ContractDetail } from '../types';

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

/**
 * 계약서 문서 모달 V2
 *
 * DocumentViewer를 사용하여 통합 UI 제공
 * - 줌/드래그 기능 추가
 * - PDF iframe 지원
 */
export function ContractDocumentModal({
  open,
  onOpenChange,
  contract,
}: ContractDocumentModalProps) {
  const router = useRouter();

  // 수정
  const handleEdit = () => {
    onOpenChange(false);
    router.push(`/ko/construction/project/contract/${contract.id}?mode=edit`);
  };

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

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

  return (
    <DocumentViewer
      title="계약서"
      subtitle={`${contract.partnerName} - ${contract.projectName}`}
      preset="construction"
      open={open}
      onOpenChange={onOpenChange}
      onEdit={handleEdit}
      onSubmit={handleSubmit}
    >
      <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>
    </DocumentViewer>
  );
}
