"use client";

/**
 * 수주 문서 모달 컴포넌트
 * - 계약서, 거래명세서, 발주서를 모달 형태로 표시
 */

import {
  Dialog,
  DialogContent,
  DialogTitle,
  VisuallyHidden,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import {
  X as XIcon,
  Printer,
  Share2,
  FileDown,
  Mail,
  Phone,
} from "lucide-react";
import { ContractDocument } from "./ContractDocument";
import { TransactionDocument } from "./TransactionDocument";
import { PurchaseOrderDocument } from "./PurchaseOrderDocument";
import { OrderItem } from "../ItemAddDialog";

// 문서 타입
export type OrderDocumentType = "contract" | "transaction" | "purchaseOrder";

// 문서 데이터 인터페이스
export interface OrderDocumentData {
  lotNumber: string;
  orderDate: string;
  client: string;
  siteName: string;
  manager: string;
  managerContact: string;
  deliveryRequestDate: string;
  expectedShipDate: string;
  deliveryMethod: string;
  address: string;
  items: OrderItem[];
  subtotal: number;
  discountRate: number;
  totalAmount: number;
  remarks?: string;
}

interface OrderDocumentModalProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  documentType: OrderDocumentType;
  data: OrderDocumentData;
}

export function OrderDocumentModal({
  open,
  onOpenChange,
  documentType,
  data,
}: OrderDocumentModalProps) {
  const getDocumentTitle = () => {
    switch (documentType) {
      case "contract":
        return "계약서";
      case "transaction":
        return "거래명세서";
      case "purchaseOrder":
        return "발주서";
      default:
        return "문서";
    }
  };

  const handlePrint = () => {
    window.print();
  };

  const handleSharePdf = () => {
    console.log("PDF 다운로드");
  };

  const handleShareEmail = () => {
    console.log("이메일 공유");
  };

  const handleShareFax = () => {
    console.log("팩스 전송");
  };

  const handleShareKakao = () => {
    console.log("카카오톡 공유");
  };

  const renderDocument = () => {
    switch (documentType) {
      case "contract":
        return (
          <ContractDocument
            orderNumber={data.lotNumber}
            orderDate={data.orderDate}
            client={data.client}
            items={data.items}
            subtotal={data.subtotal}
            discountRate={data.discountRate}
            totalAmount={data.totalAmount}
            remarks={data.remarks}
          />
        );
      case "transaction":
        return (
          <TransactionDocument
            orderNumber={data.lotNumber}
            orderDate={data.orderDate}
            client={data.client}
            items={data.items}
            subtotal={data.subtotal}
            discountRate={data.discountRate}
            totalAmount={data.totalAmount}
          />
        );
      case "purchaseOrder":
        return (
          <PurchaseOrderDocument
            orderNumber={data.lotNumber}
            client={data.client}
            siteName={data.siteName}
            manager={data.manager}
            managerContact={data.managerContact}
            deliveryRequestDate={data.deliveryRequestDate}
            expectedShipDate={data.expectedShipDate}
            deliveryMethod={data.deliveryMethod}
            address={data.address}
            items={data.items}
            remarks={data.remarks}
          />
        );
      default:
        return null;
    }
  };

  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>{getDocumentTitle()} 상세</DialogTitle>
        </VisuallyHidden>

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

        {/* 버튼 영역 - 고정 */}
        <div className="flex-shrink-0 flex items-center gap-2 px-6 py-3 bg-muted/30 border-b">
          <Button variant="outline" size="sm" onClick={handleSharePdf}>
            <FileDown className="h-4 w-4 mr-1" />
            PDF
          </Button>
          <Button variant="outline" size="sm" onClick={handleShareEmail}>
            <Mail className="h-4 w-4 mr-1" />
            이메일
          </Button>
          <Button variant="outline" size="sm" onClick={handleShareFax}>
            <Phone className="h-4 w-4 mr-1" />
            팩스
          </Button>
          <Button variant="outline" size="sm" onClick={handleShareKakao}>
            <Share2 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>

        {/* 문서 영역 - 스크롤 */}
        <div className="flex-1 overflow-y-auto bg-gray-100 p-6">
          <div className="max-w-[210mm] mx-auto bg-white shadow-lg rounded-lg">
            {renderDocument()}
          </div>
        </div>
      </DialogContent>
    </Dialog>
  );
}