"use client"

import * as React from "react"
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { BiddingProjects } from "@/db/schema"
import { useToast } from "@/hooks/use-toast"

// Import the function
import { getProjectSeriesForProject } from "../service"

// Define the ProjectSeries type based on the schema
interface ProjectSeries {
  pspid: string;
  sersNo: string;
  scDt?: string | null;
  klDt?: string | null;
  lcDt?: string | null;
  dlDt?: string | null;
  dockNo?: string | null;
  dockNm?: string | null;
  projNo?: string | null;
  post1?: string | null;
}

interface ProjectSeriesDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  project: BiddingProjects | null
}

export function ProjectSeriesDialog({
  open,
  onOpenChange,
  project,
}: ProjectSeriesDialogProps) {
  const { toast } = useToast()
  
  const [projectSeries, setProjectSeries] = React.useState<ProjectSeries[]>([])
  const [isLoading, setIsLoading] = React.useState(false)
  
  React.useEffect(() => {
    async function loadItems() {
      if (!project?.pspid) return;
      
      setIsLoading(true)
      try {
        const result = await getProjectSeriesForProject(project.pspid)
        setProjectSeries(result)
      } catch (error) {
        console.error("프로젝트 시리즈 로드 오류:", error)
        toast({
          title: "오류",
          description: "프로젝트 시리즈 로드 실패",
          variant: "destructive",
        })
      } finally {
        setIsLoading(false)
      }
    }
    
    if (open && project) {
      loadItems()
    }
  }, [toast, project, open])

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[900px]">
        <DialogHeader>
          <DialogTitle>
            {project ? `시리즈 목록 - ${project.projNm || project.pspid}` : "시리즈 목록"}
          </DialogTitle>
        </DialogHeader>
        {isLoading ? (
          <div className="flex items-center justify-center h-40">
            로딩 중...
          </div>
        ) : (
          <div className="max-h-[500px] overflow-y-auto">
            <Table>
              <TableHeader>
                <TableRow>
                  <TableHead>시리즈번호</TableHead>
                  <TableHead>K/L 연도분기</TableHead>
                  <TableHead>도크코드</TableHead>
                  <TableHead>도크명</TableHead>
                  <TableHead>SN공사번호</TableHead>
                  <TableHead>SN공사명</TableHead>
                </TableRow>
              </TableHeader>
              <TableBody>
                {projectSeries && projectSeries.length > 0 ? (
                  projectSeries.map((series) => (
                    <TableRow key={`${series.pspid}-${series.sersNo}`}>
                      <TableCell>{series.sersNo}</TableCell>
                      <TableCell>{series.scDt}</TableCell>
                      <TableCell>{series.klDt}</TableCell>
                      <TableCell>{series.lcDt}</TableCell>
                      <TableCell>{series.dlDt}</TableCell>
                      <TableCell>{series.dockNo}</TableCell>
                      <TableCell>{series.dockNm}</TableCell>
                      <TableCell>{series.projNo}</TableCell>
                      <TableCell>{series.post1}</TableCell>
                    </TableRow>
                  ))
                ) : (
                  <TableRow>
                    <TableCell colSpan={6} className="text-center h-24">
                      시리즈 데이터가 없습니다.
                    </TableCell>
                  </TableRow>
                )}
              </TableBody>
            </Table>
          </div>
        )}
      </DialogContent>
    </Dialog>
  )
}