'use client';

import { DocumentViewer } from '@/components/document-system';
import { ProposalDocument } from './ProposalDocument';
import { ExpenseReportDocument } from './ExpenseReportDocument';
import { ExpenseEstimateDocument } from './ExpenseEstimateDocument';
import { LinkedDocumentContent } from './LinkedDocumentContent';
import { DynamicDocument } from './DynamicDocument';
import type {
  DocumentDetailModalProps,
  ProposalDocumentData,
  ExpenseReportDocumentData,
  ExpenseEstimateDocumentData,
  LinkedDocumentData,
  DynamicDocumentData,
} from './types';

/**
 * 문서 상세 모달 V2
 *
 * DocumentViewer를 사용하여 통합 UI 제공
 * - 줌/드래그 기능 추가
 * - 모드에 따른 버튼 자동 설정
 */
export function DocumentDetailModalV2({
  open,
  onOpenChange,
  documentType,
  data,
  mode = 'inbox',
  documentStatus,
  onEdit,
  onCopy,
  onApprove,
  onReject,
  onSubmit,
}: DocumentDetailModalProps) {
  // 문서 타입별 제목
  const getDocumentTitle = () => {
    switch (documentType) {
      case 'proposal':
        return '품의서';
      case 'expenseReport':
        return '지출결의서';
      case 'expenseEstimate':
        return '비용견적서';
      case 'document':
        return (data as LinkedDocumentData).templateName || '문서 결재';
      case 'dynamic':
        return (data as DynamicDocumentData).formName || '문서';
      default:
        return '문서';
    }
  };

  // 모드에 따른 프리셋 결정
  const getPreset = () => {
    // 기안함 모드 + 임시저장 상태: 복제, 상신, 인쇄
    if (mode === 'draft' && documentStatus === 'draft') {
      return 'approval-draft' as const;
    }
    // 결재함 모드: 수정, 반려, 승인, 인쇄
    if (mode === 'inbox') {
      return 'approval-inbox' as const;
    }
    // 그 외 (참조함 등): 인쇄만
    return 'readonly' as const;
  };

  // 문서 콘텐츠 렌더링
  const renderDocument = () => {
    switch (documentType) {
      case 'proposal':
        return <ProposalDocument data={data as ProposalDocumentData} />;
      case 'expenseReport':
        return <ExpenseReportDocument data={data as ExpenseReportDocumentData} />;
      case 'expenseEstimate':
        return <ExpenseEstimateDocument data={data as ExpenseEstimateDocumentData} />;
      case 'document':
        return <LinkedDocumentContent data={data as LinkedDocumentData} />;
      case 'dynamic':
        return <DynamicDocument data={data as DynamicDocumentData} />;
      default:
        return null;
    }
  };

  return (
    <DocumentViewer
      title={getDocumentTitle()}
      subtitle={`${getDocumentTitle()} 상세`}
      preset={getPreset()}
      open={open}
      onOpenChange={onOpenChange}
      onEdit={onEdit}
      onCopy={onCopy}
      onApprove={onApprove}
      onReject={onReject}
      onSubmit={onSubmit}
    >
      {renderDocument()}
    </DocumentViewer>
  );
}
