import { Bidding } from '@/db/schema/bidding'
import { Building2, Package, User, DollarSign, Calendar } from 'lucide-react'
import { contractTypeLabels, biddingTypeLabels } from '@/db/schema/bidding'

interface BiddingInfoHeaderProps {
  bidding: Bidding
}

function formatDate(date: Date | string | null | undefined, locale: 'KR' | 'EN' = 'KR'): string {
  if (!date) return ''
  
  const dateObj = typeof date === 'string' ? new Date(date) : date
  
  if (locale === 'KR') {
    return dateObj.toLocaleDateString('ko-KR', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    }).replace(/\./g, '-').replace(/-$/, '')
  }
  
  return dateObj.toLocaleDateString('en-US', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

export function BiddingInfoHeader({ bidding }: BiddingInfoHeaderProps) {
  return (
    <div className="bg-white border rounded-lg p-6 mb-6 shadow-sm">
      {/* 주요 정보 섹션 */}
      <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-6">
        {/* 프로젝트 정보 */}
        {bidding.projectName && (
          <div className="space-y-1">
            <div className="flex items-center gap-2 text-sm text-gray-500">
              <Building2 className="w-4 h-4" />
              <span>프로젝트</span>
            </div>
            <div className="font-medium text-gray-900">{bidding.projectName}</div>
          </div>
        )}

        {/* 품목 정보 */}
        {bidding.itemName && (
          <div className="space-y-1">
            <div className="flex items-center gap-2 text-sm text-gray-500">
              <Package className="w-4 h-4" />
              <span>품목</span>
            </div>
            <div className="font-medium text-gray-900">{bidding.itemName}</div>
          </div>
        )}

        {/* 담당자 정보 */}
        {bidding.managerName && (
          <div className="space-y-1">
            <div className="flex items-center gap-2 text-sm text-gray-500">
              <User className="w-4 h-4" />
              <span>담당자</span>
            </div>
            <div className="font-medium text-gray-900">{bidding.managerName}</div>
          </div>
        )}

        {/* 예산 정보 */}
        {bidding.budget && (
          <div className="space-y-1">
            <div className="flex items-center gap-2 text-sm text-gray-500">
              <DollarSign className="w-4 h-4" />
              <span>예산</span>
            </div>
            <div className="font-semibold text-gray-900">
              {new Intl.NumberFormat('ko-KR', {
                style: 'currency',
                currency: bidding.currency || 'KRW',
              }).format(Number(bidding.budget))}
            </div>
          </div>
        )}
      </div>

      {/* 구분선 */}
      <div className="border-t border-gray-100 pt-4 mb-4">
        {/* 계약 정보 */}
        <div className="flex flex-wrap gap-8 text-sm">
          <div className="flex items-center gap-2">
            <span className="text-gray-500">계약</span>
            <span className="font-medium">{contractTypeLabels[bidding.contractType]}</span>
          </div>
          
          <div className="flex items-center gap-2">
            <span className="text-gray-500">유형</span>
            <span className="font-medium">{biddingTypeLabels[bidding.biddingType]}</span>
          </div>

          <div className="flex items-center gap-2">
            <span className="text-gray-500">낙찰</span>
            <span className="font-medium">{bidding.awardCount === 'single' ? '단수' : '복수'}</span>
          </div>

          <div className="flex items-center gap-2">
            <span className="text-gray-500">통화</span>
            <span className="font-mono font-medium">{bidding.currency}</span>
          </div>
        </div>
      </div>

      {/* 일정 정보 */}
      {(bidding.submissionStartDate || bidding.evaluationDate || bidding.preQuoteDate || bidding.biddingRegistrationDate) && (
        <div className="border-t border-gray-100 pt-4">
          <div className="flex items-center gap-2 mb-3 text-sm text-gray-500">
            <Calendar className="w-4 h-4" />
            <span>일정 정보</span>
          </div>
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
            {bidding.submissionStartDate && bidding.submissionEndDate && (
              <div>
                <span className="text-gray-500">제출기간</span>
                <div className="font-medium">
                  {formatDate(bidding.submissionStartDate, 'KR')} ~ {formatDate(bidding.submissionEndDate, 'KR')}
                </div>
              </div>
            )}
            {bidding.biddingRegistrationDate && (
              <div>
                <span className="text-gray-500">입찰등록일</span>
                <div className="font-medium">{formatDate(bidding.biddingRegistrationDate, 'KR')}</div>
              </div>
            )}
            {bidding.preQuoteDate && (
              <div>
                <span className="text-gray-500">사전견적일</span>
                <div className="font-medium">{formatDate(bidding.preQuoteDate, 'KR')}</div>
              </div>
            )}
            {bidding.evaluationDate && (
              <div>
                <span className="text-gray-500">평가일</span>
                <div className="font-medium">{formatDate(bidding.evaluationDate, 'KR')}</div>
              </div>
            )}
          </div>
        </div>
      )}
    </div>
  )
}
