"use client";

import * as React from "react";
import { useRouter } from "next/navigation";
import { DataTable } from "@/components/data-table/data-table";
import { useDataTable } from "@/hooks/use-data-table";
import { DataTableAdvancedToolbar } from "@/components/data-table/data-table-advanced-toolbar";
import type {
  DataTableAdvancedFilterField,
  DataTableRowAction,
} from "@/types/table"
import { getProjectGtcList } from "../service";
import { getColumns } from "./project-gtc-table-columns";
import { DeleteGtcFileDialog } from "./delete-gtc-file-dialog";
import { UpdateGtcFileSheet } from "./update-gtc-file-sheet";
import { ProjectGtcTableToolbarActions } from "./project-gtc-table-toolbar-actions";
import { ProjectGtcView } from "@/db/schema";

interface ProjectGtcTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getProjectGtcList>>,
    ]
  >
}

export function ProjectGtcTable({ promises }: ProjectGtcTableProps) {
  const router = useRouter();
  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<ProjectGtcView> | null>(null)

  const [{ data, pageCount }] =
    React.use(promises)

  // 컬럼 설정 - 외부 파일에서 가져옴
  const columns = React.useMemo(
    () => getColumns({ setRowAction }),
    [setRowAction]
  )

  // config 기반으로 필터 필드 설정
  const advancedFilterFields: DataTableAdvancedFilterField<ProjectGtcView>[] = [
    { id: "code", label: "프로젝트 코드", type: "text" },
    { id: "name", label: "프로젝트명", type: "text" },
    {
      id: "type", label: "프로젝트 타입", type: "select", options: [
        { label: "Ship", value: "ship" },
        { label: "Offshore", value: "offshore" },
        { label: "Other", value: "other" },
      ]
    },
    { id: "originalFileName", label: "GTC 파일명", type: "text" },
    { id: "projectCreatedAt", label: "프로젝트 생성일", type: "date" },
    { id: "gtcCreatedAt", label: "GTC 등록일", type: "date" },
  ];

  const { table } = useDataTable({
      data,
      columns,
      pageCount,
      enablePinning: true,
      enableAdvancedFilter: true,
      initialState: {
        sorting: [{ id: "projectCreatedAt", desc: true }],
        columnPinning: { right: ["actions"] },
      },
      getRowId: (originalRow) => String(originalRow.id),
      shallow: false,
      clearOnDefault: true,
    })

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
        >
          <ProjectGtcTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>

      <DeleteGtcFileDialog
        open={rowAction?.type === "delete"}
        onOpenChange={() => setRowAction(null)}
        projects={rowAction?.row.original ? [rowAction?.row.original] : []}
        showTrigger={false}
        onSuccess={() => {
          router.refresh();
        }}
      />

      <UpdateGtcFileSheet
        open={rowAction?.type === "upload"}
        onOpenChange={() => setRowAction(null)}
        project={rowAction && rowAction.type === "upload" ? rowAction.row.original : null}
      />
    </>
  );
} 