"use client"

import { type Table } from "@tanstack/react-table"
import { Download, RotateCcw, BarChart3, Filter } from "lucide-react"
import { useRouter } from "next/navigation"
import { useTransition } from "react"

import { Button } from "@/components/ui/button"
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"

import { ExtendedPageVisit } from "../validation"
import { exportTableToExcel } from "@/lib/export_all"

interface PageVisitsTableToolbarActionsProps {
  table: Table<ExtendedPageVisit>
}

export function PageVisitsTableToolbarActions({
  table,
}: PageVisitsTableToolbarActionsProps) {
  const router = useRouter()
  const [isPending, startTransition] = useTransition()

  const handleRefresh = () => {
    startTransition(() => {
      router.refresh() // ✅ 서버 컴포넌트만 새로고침 (더 빠르고 부드러움)
    })
  }

  return (
    <div className="flex items-center gap-2">
      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant="outline"
            size="sm"
            onClick={() =>
              exportTableToExcel(table, {
                filename: "page-visits",
                excludeColumns: ["select", "actions"],
              })
            }
          >
            <Download className="mr-2 size-4" aria-hidden="true" />
            Export
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          <p>페이지 방문 데이터를 엑셀로 내보내기</p>
        </TooltipContent>
      </Tooltip>

      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant="outline"
            size="sm"
            onClick={handleRefresh}
            disabled={isPending} // 로딩 중 비활성화
          >
            <RotateCcw 
              className={`mr-2 size-4 ${isPending ? 'animate-spin' : ''}`} 
              aria-hidden="true" 
            />
            {isPending ? '새로고침 중...' : '새로고침'}
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          <p>데이터 새로고침</p>
        </TooltipContent>
      </Tooltip>

      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant="outline"
            size="sm"
            onClick={() => {
              // 페이지 통계 분석 기능
              console.log("Generate page analytics")
            }}
          >
            <BarChart3 className="mr-2 size-4" aria-hidden="true" />
            페이지 분석
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          <p>페이지별 방문 통계 분석</p>
        </TooltipContent>
      </Tooltip>

      <Tooltip>
        <TooltipTrigger asChild>
          <Button
            variant="outline"
            size="sm"
            onClick={() => {
              // 고급 필터링 옵션
              console.log("Advanced filtering options")
            }}
          >
            <Filter className="mr-2 size-4" aria-hidden="true" />
            고급 필터
          </Button>
        </TooltipTrigger>
        <TooltipContent>
          <p>고급 필터링 옵션</p>
        </TooltipContent>
      </Tooltip>
    </div>
  )
}
