'use client'

import * as React from 'react'
import { useSession } from 'next-auth/react'
import { type DataTableAdvancedFilterField, type DataTableFilterField } from '@/types/table'
import { useDataTable } from '@/hooks/use-data-table'
import { DataTable } from '@/components/data-table/data-table'
import { DataTableAdvancedToolbar } from '@/components/data-table/data-table-advanced-toolbar'
import { BiddingDetailVendorToolbarActions } from './bidding-detail-vendor-toolbar-actions'
import { BiddingDetailVendorEditDialog } from './bidding-detail-vendor-edit-dialog'
import { BiddingAwardDialog } from './bidding-award-dialog'
import { getBiddingDetailVendorColumns } from './bidding-detail-vendor-columns'
import { QuotationVendor, getPriceAdjustmentFormByBiddingCompanyId } from '@/lib/bidding/detail/service'
import { Bidding } from '@/db/schema'
import { PriceAdjustmentDialog } from '@/components/bidding/price-adjustment-dialog'
import { useToast } from '@/hooks/use-toast'

interface BiddingDetailVendorTableContentProps {
  biddingId: number
  bidding: Bidding
  vendors: QuotationVendor[]
  onRefresh: () => void
  onOpenTargetPriceDialog: () => void
  onOpenSelectionReasonDialog: () => void
  onEdit?: (vendor: QuotationVendor) => void
  onViewItemDetails?: (vendor: QuotationVendor) => void
}

const filterFields: DataTableFilterField<QuotationVendor>[] = [
  {
    id: 'vendorName',
    label: '업체명',
    placeholder: '업체명으로 검색...',
  },
  {
    id: 'vendorCode',
    label: '업체코드',
    placeholder: '업체코드로 검색...',
  },
  {
    id: 'contactPerson',
    label: '담당자',
    placeholder: '담당자로 검색...',
  },
]

const advancedFilterFields: DataTableAdvancedFilterField<QuotationVendor>[] = [
  {
    id: 'vendorName',
    label: '업체명',
    type: 'text',
  },
  {
    id: 'vendorCode',
    label: '업체코드',
    type: 'text',
  },
  {
    id: 'contactPerson',
    label: '담당자',
    type: 'text',
  },
  {
    id: 'quotationAmount',
    label: '견적금액',
    type: 'number',
  },
  {
    id: 'status',
    label: '상태',
    type: 'multi-select',
    options: [
      { label: '제출완료', value: 'submitted' },
      { label: '선정완료', value: 'selected' },
      { label: '미제출', value: 'pending' },
    ],
  },
]

export function BiddingDetailVendorTableContent({
  biddingId,
  bidding,
  vendors,
  onRefresh,
  onOpenTargetPriceDialog,
  onEdit,
  onViewItemDetails
}: BiddingDetailVendorTableContentProps) {
  const { data: session } = useSession()
  const { toast } = useToast()
  
  // 세션에서 사용자 ID 가져오기
  const userId = session?.user?.id || ''
  const [selectedVendor, setSelectedVendor] = React.useState<QuotationVendor | null>(null)
  const [isEditDialogOpen, setIsEditDialogOpen] = React.useState(false)
  const [isAwardDialogOpen, setIsAwardDialogOpen] = React.useState(false)
  const [priceAdjustmentData, setPriceAdjustmentData] = React.useState<any>(null)
  const [isPriceAdjustmentDialogOpen, setIsPriceAdjustmentDialogOpen] = React.useState(false)

  const handleEdit = (vendor: QuotationVendor) => {
    setSelectedVendor(vendor)
    setIsEditDialogOpen(true)
  }

  const handleViewPriceAdjustment = async (vendor: QuotationVendor) => {
    try {
      const priceAdjustmentForm = await getPriceAdjustmentFormByBiddingCompanyId(vendor.id)
      if (priceAdjustmentForm) {
        setPriceAdjustmentData(priceAdjustmentForm)
        setSelectedVendor(vendor)
        setIsPriceAdjustmentDialogOpen(true)
      } else {
        toast({
          title: '연동제 정보 없음',
          description: '해당 업체의 연동제 정보가 없습니다.',
          variant: 'default',
        })
      }
    } catch (error) {
      console.error('Failed to load price adjustment form:', error)
      toast({
        title: '오류',
        description: '연동제 정보를 불러오는데 실패했습니다.',
        variant: 'destructive',
      })
    }
  }

  const columns = React.useMemo(
    () => getBiddingDetailVendorColumns({
      onEdit: onEdit || handleEdit,
      onViewPriceAdjustment: handleViewPriceAdjustment,
      onViewItemDetails: onViewItemDetails,
      biddingStatus: bidding.status
    }),
    [onEdit, handleEdit, handleViewPriceAdjustment, onViewItemDetails, bidding.status]
  )

  const { table } = useDataTable({
    data: vendors,
    columns,
    pageCount: 1,
    filterFields,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: 'vendorName', desc: false }],
      columnPinning: { right: ['actions'] },
    },
    getRowId: (originalRow) => originalRow.id.toString(),
    shallow: false,
    clearOnDefault: true,
  })

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
          shallow={false}
        >
          <BiddingDetailVendorToolbarActions
            biddingId={biddingId}
            bidding={bidding}
            userId={userId}
            onOpenTargetPriceDialog={onOpenTargetPriceDialog}
            onOpenAwardDialog={() => setIsAwardDialogOpen(true)}
            onSuccess={onRefresh}
          />
        </DataTableAdvancedToolbar>
      </DataTable>

      <BiddingDetailVendorEditDialog
        vendor={selectedVendor}
        open={isEditDialogOpen}
        onOpenChange={setIsEditDialogOpen}
        onSuccess={onRefresh}
        biddingAwardCount={bidding.awardCount}
        biddingStatus={bidding.status}
      />

      <BiddingAwardDialog
        biddingId={biddingId}
        open={isAwardDialogOpen}
        onOpenChange={setIsAwardDialogOpen}
        onSuccess={onRefresh}
      />

      <PriceAdjustmentDialog
        open={isPriceAdjustmentDialogOpen}
        onOpenChange={setIsPriceAdjustmentDialogOpen}
        data={priceAdjustmentData}
        vendorName={selectedVendor?.vendorName || ''}
      />
    </>
  )
}
