"use client"

import * as React from "react"
import type {
  DataTableAdvancedFilterField,
  DataTableFilterField,
  DataTableRowAction,
} 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 { getPageVisits } from "../service"
import { PageVisitsTableToolbarActions } from "./page-visits-table-toolbar-actions"
import { getColumns } from "./page-visits-table-columns"
import { ExtendedPageVisit } from "../validation"

interface PageVisitsTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getPageVisits>>,
    ]
  >
}

export function PageVisitsTable({ promises }: PageVisitsTableProps) {

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

  const [rowAction, setRowAction] =
    React.useState<DataTableRowAction<ExtendedPageVisit> | null>(null)

  const columns = React.useMemo(
    () => getColumns({ setRowAction }),
    [setRowAction]
  )

  // 기본 필터 필드
  const filterFields: DataTableFilterField<ExtendedPageVisit>[] = [
    {
      id: "deviceType",
      label: "디바이스",
      options: [
        { label: "Desktop", value: "desktop" },
        { label: "Mobile", value: "mobile" },
        { label: "Tablet", value: "tablet" },
      ],
    },
    {
      id: "browserName",
      label: "브라우저",
      options: [
        { label: "Chrome", value: "Chrome" },
        { label: "Firefox", value: "Firefox" },
        { label: "Safari", value: "Safari" },
        { label: "Edge", value: "Edge" },
      ],
    },
  ]

  // 고급 필터 필드
  const advancedFilterFields: DataTableAdvancedFilterField<ExtendedPageVisit>[] = [
    {
      id: "userEmail",
      label: "사용자 이메일",
      type: "text",
    },
    {
      id: "route",
      label: "페이지 경로",
      type: "text",
    },
    {
      id: "pageTitle",
      label: "페이지 제목",
      type: "text",
    },
    {
      id: "deviceType",
      label: "디바이스 타입",
      type: "multi-select",
      options: [
        { label: "Desktop", value: "desktop" },
        { label: "Mobile", value: "mobile" },
        { label: "Tablet", value: "tablet" },
      ],
    },
    {
      id: "browserName",
      label: "브라우저",
      type: "multi-select",
      options: [
        { label: "Chrome", value: "Chrome" },
        { label: "Firefox", value: "Firefox" },
        { label: "Safari", value: "Safari" },
        { label: "Edge", value: "Edge" },
      ],
    },
    {
      id: "duration",
      label: "체류 시간 (초)",
      type: "number",
    },
    {
      id: "visitedAt",
      label: "방문 시간",
      type: "date",
    },
    {
      id: "ipAddress",
      label: "IP 주소",
      type: "text",
    },
  ]

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

  return (
    <>
      <DataTable table={table}>
        <DataTableAdvancedToolbar
          table={table}
          filterFields={advancedFilterFields}
          shallow={false}
        >
          <PageVisitsTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>
    </>
  )
}