// components/portal/MaterialsSwitcher.tsx
"use client";

import { useRouter, useSearchParams } from "next/navigation";
import dynamic from "next/dynamic";
import { cn } from "@/lib/utils";

// Reuse the *existing* canonical pages.
// Adjust paths if your folders differ.
const MaterialsPanel = dynamic(() => import("@/app/materials/materials/page"), { ssr: false });
const CoatingsPanel  = dynamic(() => import("@/app/materials/materials-coatings/page"), { ssr: false });

const TABS = [
    { key: "materials",         label: "Materials" },
{ key: "materials-coatings", label: "Coatings" },
];

function Panel({ tab }: { tab: string }) {
    switch (tab) {
        case "materials":          return <MaterialsPanel />;
        case "materials-coatings": return <CoatingsPanel />;
        default:                   return <MaterialsPanel />;
    }
}

export default function MaterialsSwitcher() {
    const router = useRouter();
    const sp = useSearchParams();
    const active = sp.get("t") || "materials";

    function setTab(nextKey: string) {
        const q = new URLSearchParams(sp.toString());
        q.set("t", nextKey);
        router.replace(`/portal/materials?${q.toString()}`, { scroll: false });
    }

    return (
        <div>
        <div className="mb-4 flex flex-wrap items-center gap-2">
        {TABS.map(({ key, label }) => (
            <button
            key={key}
            onClick={() => setTab(key)}
            className={cn(
                "rounded-md border px-3 py-1.5 text-sm transition",
                active === key ? "bg-primary text-primary-foreground" : "hover:bg-muted"
            )}
            >
            {label}
            </button>
        ))}
        </div>

        <div className="rounded-md border p-4">
        <Panel tab={active} />
        </div>
        </div>
    );
}
