"use client";

import { usePathname, useRouter } from "next/navigation";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";

/**
 * URL-driven tabs for the notification inbox.
 *
 * `?filter=unread` is the single source of truth so the server component
 * re-queries on change and the view is shareable/bookmarkable.
 */
export function NotificationTabs({
  filter,
  unreadCount,
  totalCount,
}: {
  filter: "all" | "unread";
  unreadCount: number;
  totalCount: number;
}) {
  const router = useRouter();
  const pathname = usePathname();

  function select(next: string) {
    const params = new URLSearchParams();
    if (next === "unread") params.set("filter", "unread");
    const search = params.toString();
    router.replace(search ? `${pathname}?${search}` : pathname, { scroll: false });
  }

  return (
    <Tabs value={filter} onValueChange={select}>
      <TabsList>
        <TabsTrigger value="all">
          Semua
          <span className="ml-1 text-xs tabular-nums text-tertiary">{totalCount}</span>
        </TabsTrigger>
        <TabsTrigger value="unread">
          Belum dibaca
          {unreadCount > 0 ? (
            <span className="ml-1 text-xs tabular-nums text-tertiary">{unreadCount}</span>
          ) : null}
        </TabsTrigger>
      </TabsList>
    </Tabs>
  );
}
