// 인수인계보고서 타입 정의

// 인수인계 상태
export type HandoverStatus = 'pending' | 'completed';

// 인수인계보고서 (목록용)
export interface HandoverReport {
  id: string;
  reportNumber: string;           // 보고서번호
  partnerId?: string;             // 거래처 ID (필터용)
  partnerName: string;            // 거래처
  siteName: string;               // 현장명
  contractManagerId?: string;     // 계약담당자 ID (필터용)
  contractManagerName: string;    // 계약담당자
  constructionPMId?: string | null; // 공사PM ID (필터용)
  constructionPMName: string | null; // 공사PM
  totalSites: number;             // 총 개소
  contractAmount: number;         // 계약금액(공급가액)
  contractStartDate: string | null; // 계약시작일
  contractEndDate: string | null;   // 계약종료일
  status: HandoverStatus;         // 상태 (인수인계대기/인수인계완료)
  contractId: string;             // 연결된 계약 ID
  createdAt: string;
  updatedAt: string;
}

// 공사담당자
export interface ConstructionManager {
  id: string;
  name: string;
  nonPerformanceReason: string;   // 미이행 사유
  signature?: string | null;      // 서명 (보고서 보기용)
}

// 계약 ITEM
export interface ContractItem {
  id: string;
  no: number;           // 번호
  name: string;         // 명칭
  product: string;      // 제품
  quantity: number;     // 수량
  remark: string;       // 비고
}

// 장비 외 실행금액
export interface ExternalEquipmentCost {
  shippingCost: number;       // 운반비
  highAltitudeWork: number;   // 고소작업대
  publicExpense: number;      // 공과잡
}

// 인수인계보고서 상세
export interface HandoverReportDetail extends HandoverReport {
  contractDate: string | null;              // 계약일자
  constructionPMId: string | null;          // 공사PM ID
  constructionManagers: ConstructionManager[]; // 공사담당자 목록
  contractItems: ContractItem[];            // 계약 ITEM 목록
  hasSecondaryPiping: boolean;              // 2차 배관 유무
  secondaryPipingAmount: number;            // 2차 배관 금액
  secondaryPipingNote: string;              // 2차 배관 비고 (포함 시 입력)
  hasCoating: boolean;                      // 도장 & 코킹 유무
  coatingAmount: number;                    // 도장 & 코킹 금액
  coatingNote: string;                      // 도장 & 코킹 비고 (포함 시 입력)
  externalEquipmentCost: ExternalEquipmentCost; // 장비 외 실행금액
  specialNotes: string;                     // 특이사항
  completionDate: string | null;            // 준공일
}

// 인수인계보고서 폼 데이터
export interface HandoverReportFormData {
  reportNumber: string;
  partnerName: string;
  siteName: string;
  contractManagerName: string;
  contractDate: string;
  totalSites: number;
  contractStartDate: string;
  contractEndDate: string;
  contractAmount: number;
  constructionPMId: string;
  constructionPMName: string;
  status: HandoverStatus;
  constructionManagers: ConstructionManager[];
  contractItems: ContractItem[];
  hasSecondaryPiping: boolean;
  secondaryPipingNote: string;
  hasCoating: boolean;
  coatingNote: string;
  externalEquipmentCost: ExternalEquipmentCost;
  specialNotes: string;
}

// 기본 폼 데이터 생성
export function getEmptyHandoverReportFormData(): HandoverReportFormData {
  return {
    reportNumber: '',
    partnerName: '',
    siteName: '',
    contractManagerName: '',
    contractDate: '',
    totalSites: 0,
    contractStartDate: '',
    contractEndDate: '',
    contractAmount: 0,
    constructionPMId: '',
    constructionPMName: '',
    status: 'pending',
    constructionManagers: [],
    contractItems: [],
    hasSecondaryPiping: false,
    secondaryPipingNote: '',
    hasCoating: false,
    coatingNote: '',
    externalEquipmentCost: {
      shippingCost: 0,
      highAltitudeWork: 0,
      publicExpense: 0,
    },
    specialNotes: '',
  };
}

// 상세 데이터를 폼 데이터로 변환
export function handoverReportDetailToFormData(detail: HandoverReportDetail): HandoverReportFormData {
  return {
    reportNumber: detail.reportNumber,
    partnerName: detail.partnerName,
    siteName: detail.siteName,
    contractManagerName: detail.contractManagerName,
    contractDate: detail.contractDate || '',
    totalSites: detail.totalSites,
    contractStartDate: detail.contractStartDate || '',
    contractEndDate: detail.contractEndDate || '',
    contractAmount: detail.contractAmount,
    constructionPMId: detail.constructionPMId || '',
    constructionPMName: detail.constructionPMName || '',
    status: detail.status,
    constructionManagers: detail.constructionManagers,
    contractItems: detail.contractItems,
    hasSecondaryPiping: detail.hasSecondaryPiping,
    secondaryPipingNote: detail.secondaryPipingNote || '',
    hasCoating: detail.hasCoating,
    coatingNote: detail.coatingNote || '',
    externalEquipmentCost: detail.externalEquipmentCost,
    specialNotes: detail.specialNotes,
  };
}

// 공사PM 목록 (목업)
export const CONSTRUCTION_PM_OPTIONS = [
  { value: 'pm1', label: '김PM' },
  { value: 'pm2', label: '이PM' },
  { value: 'pm3', label: '박PM' },
  { value: 'pm4', label: '최PM' },
];

// 담당자 목록 (목업)
export const MANAGER_OPTIONS = [
  { value: 'mgr1', label: '홍길동' },
  { value: 'mgr2', label: '김철수' },
  { value: 'mgr3', label: '이영희' },
  { value: 'mgr4', label: '박민수' },
];

// 통계
export interface HandoverReportStats {
  total: number;      // 전체 인수인계보고서
  pending: number;    // 인수인계대기
  completed: number;  // 인수인계완료
}

// 상태 필터 옵션
export const REPORT_STATUS_OPTIONS = [
  { value: 'all', label: '전체' },
  { value: 'pending', label: '인수인계대기' },
  { value: 'completed', label: '인수인계완료' },
];

// 정렬 옵션
export const REPORT_SORT_OPTIONS = [
  { value: 'contractDateDesc', label: '최신순 (계약시작일)' },
  { value: 'contractDateAsc', label: '등록순 (계약종료시작일)' },
  { value: 'partnerNameAsc', label: '거래처명 오름차순' },
  { value: 'partnerNameDesc', label: '거래처명 내림차순' },
  { value: 'siteNameAsc', label: '현장명 오름차순' },
  { value: 'siteNameDesc', label: '현장명 내림차순' },
];

// 인수인계 상태 라벨
export const HANDOVER_STATUS_LABELS: Record<HandoverStatus, string> = {
  pending: '인수인계대기',
  completed: '인수인계완료',
};

// 인수인계 상태 스타일
export const HANDOVER_STATUS_STYLES: Record<HandoverStatus, string> = {
  pending: 'inline-flex items-center justify-center rounded-md border border-orange-500 px-2 py-0.5 text-xs font-medium text-orange-600 bg-orange-50',
  completed: 'inline-flex items-center justify-center rounded-md border border-green-500 px-2 py-0.5 text-xs font-medium text-green-600 bg-green-50',
};

// 단계 탭 타입
export type StageTab = 'all' | 'estimate_selected' | 'estimate_progress' | 'delivery' | 'installation' | 'other';

// 단계 탭 데이터
export const STAGE_TABS: { key: StageTab; label: string }[] = [
  { key: 'all', label: '상태단건' },
  { key: 'estimate_selected', label: '견적일' },
  { key: 'estimate_progress', label: '발일' },
  { key: 'delivery', label: '발행' },
  { key: 'installation', label: '미체' },
  { key: 'other', label: '옵을' },
];