// pages/policy/[companyname].js
import { useMemo } from "react";
import { useRouter } from "next/router";
import { getCompanyBySlug } from "@/utils/Global";

import HepiyiImporter from "@/Components/Policy/Companies/HepiyiImporter";
import QuickImporter from "@/Components/Policy/Companies/QuickImporter";

export default function CompanyPolicyImportPage() {
  const router = useRouter();
  const slug = String(router.query.companyname || "");

  const company = useMemo(() => getCompanyBySlug(slug), [slug]);

  // pick importer by slug
  const Importer = useMemo(() => {
    if (!company) return null;
    if (company.slug === "hepiyi-sigorta") return HepiyiImporter;
    if (company.slug === "quick-sigorta") return QuickImporter;
    return null;
  }, [company]);

  if (!company) {
    return (
      <div className="min-h-screen bg-[#fafafa] p-6">
        <div className="rounded-3xl border border-black/10 bg-white shadow-sm p-6">
          <div className="text-lg font-extrabold text-black/85">Şirket bulunamadı</div>
          <div className="mt-1 text-sm text-black/55">
            URL: <span className="font-semibold">{slug || "—"}</span>
          </div>
          <button
            onClick={() => router.push("/Policy")}
            className="mt-4 rounded-2xl border border-black/10 bg-white px-4 py-3 text-sm font-semibold text-black/70 hover:bg-black/[0.03] transition"
          >
            ← Poliçeler
          </button>
        </div>
      </div>
    );
  }

  if (!Importer) {
    return (
      <div className="min-h-screen bg-[#fafafa] p-6">
        <div className="rounded-3xl border border-black/10 bg-white shadow-sm p-6">
          <div className="text-lg font-extrabold text-black/85">
            Bu şirket için importer tanımlı değil
          </div>
          <div className="mt-1 text-sm text-black/55">
            Şirket: <span className="font-semibold">{company.name}</span>
          </div>
          <button
            onClick={() => router.push("/Policy")}
            className="mt-4 rounded-2xl border border-black/10 bg-white px-4 py-3 text-sm font-semibold text-black/70 hover:bg-black/[0.03] transition"
          >
            ← Poliçeler
          </button>
        </div>
      </div>
    );
  }

  return <Importer company={company} />;
}