'use client';

import { useEffect, useState } from 'react';
import { useSearchParams } from 'next/navigation';
import ContractDetailForm from '@/components/business/construction/contract/ContractDetailForm';
import { getContractDetail } from '@/components/business/construction/contract';
import type { ContractDetail } from '@/components/business/construction/contract/types';
import { GenericPageSkeleton } from '@/components/ui/skeleton';

export default function ContractCreatePage() {
  const searchParams = useSearchParams();
  const baseContractId = searchParams.get('baseContractId');

  const [baseData, setBaseData] = useState<ContractDetail | undefined>(undefined);
  const [isLoading, setIsLoading] = useState(!!baseContractId);

  useEffect(() => {
    if (baseContractId) {
      // 변경 계약서 생성: 기존 계약 데이터 복사
      getContractDetail(baseContractId)
        .then(result => {
          if (result.success && result.data) {
            setBaseData(result.data);
          }
        })
        .finally(() => setIsLoading(false));
    }
  }, [baseContractId]);

  if (isLoading) return <GenericPageSkeleton />;

  return (
    <ContractDetailForm
      mode="create"
      contractId=""
      initialData={baseData}
      isChangeContract={!!baseContractId}
    />
  );
}