"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 { getLoginSessions } from "../service"
import { LoginSessionsTableToolbarActions } from "./login-sessions-table-toolbar-actions"
import { getColumns } from "./login-sessions-table-columns"
import { ExtendedLoginSession } from "../validation"

interface LoginSessionsTableProps {
  promises: Promise<
    [
      Awaited<ReturnType<typeof getLoginSessions>>,
    ]
  >
}

export function LoginSessionsTable({ promises }: LoginSessionsTableProps) {
  const [{ data, pageCount }] = React.use(promises)

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

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

  // 기본 필터 필드
  const filterFields: DataTableFilterField<ExtendedLoginSession>[] = [
    {
      id: "authMethod",
      label: "인증 방식",
      options: [
        { label: "OTP", value: "otp" },
        { label: "Email", value: "email" },
        { label: "SGIPS", value: "sgips" },
        { label: "SAML", value: "saml" },
      ],
    },
    {
      id: "isActive",
      label: "세션 상태",
      options: [
        { label: "활성", value: "true" },
        { label: "비활성", value: "false" },
      ],
    },
  ]

  // 고급 필터 필드
  const advancedFilterFields: DataTableAdvancedFilterField<ExtendedLoginSession>[] = [
    {
      id: "userEmail",
      label: "사용자 이메일",
      type: "text",
    },
    {
      id: "userName", 
      label: "사용자 이름",
      type: "text",
    },
    {
      id: "authMethod",
      label: "인증 방식",
      type: "multi-select",
      options: [
        { label: "OTP", value: "otp" },
        { label: "Email", value: "email" },
        { label: "SGIPS", value: "sgips" },
        { label: "SAML", value: "saml" },
      ],
    },
    {
      id: "ipAddress",
      label: "IP 주소",
      type: "text",
    },
    {
      id: "isActive",
      label: "활성 상태",
      type: "boolean",
    },
    {
      id: "loginAt",
      label: "로그인 시간",
      type: "date",
    },
    {
      id: "logoutAt",
      label: "로그아웃 시간", 
      type: "date",
    },
  ]

  const { table } = useDataTable({
    data,
    columns,
    pageCount,
    filterFields,
    enablePinning: true,
    enableAdvancedFilter: true,
    initialState: {
      sorting: [{ id: "loginAt", 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}
        >
          <LoginSessionsTableToolbarActions table={table} />
        </DataTableAdvancedToolbar>
      </DataTable>
    </>
  )
}