import Link from "next/link";
import { Edit, ExternalLink, Plus } from "lucide-react";
import { PageHeader } from "@/components/admin/form";
import { DeleteButton } from "@/components/admin/delete-button";
import { listIndustries } from "@/lib/data";
import { deleteIndustry } from "@/lib/admin-actions";

export default async function IndustriesAdminPage() {
  const items = await listIndustries();
  return (
    <div>
      <PageHeader
        title="Sektörler"
        description="SEO odaklı sektör landing sayfaları. /sektor/[slug] üzerinden yayınlanır."
        action={
          <Link
            href="/admin/sektorler/new"
            className="inline-flex items-center gap-2 rounded-full bg-[var(--navy)] px-4 py-2 text-sm font-medium text-white transition hover:bg-[var(--navy-700)]"
          >
            <Plus className="size-4" /> Yeni sektör
          </Link>
        }
      />
      <div className="mt-6 overflow-hidden rounded-2xl border border-[var(--border)] bg-white">
        <table className="w-full text-sm">
          <thead className="bg-[var(--navy-50)] text-xs uppercase tracking-wider text-[var(--muted)]">
            <tr>
              <th className="px-4 py-3 text-left">Sıra</th>
              <th className="px-4 py-3 text-left">Başlık</th>
              <th className="px-4 py-3 text-left">Slug</th>
              <th className="px-4 py-3 text-left">Öne çıkan</th>
              <th className="px-4 py-3 text-right">İşlem</th>
            </tr>
          </thead>
          <tbody>
            {items.length === 0 && (
              <tr>
                <td colSpan={5} className="px-4 py-12 text-center text-[var(--muted)]">
                  Henüz sektör eklenmemiş.
                </td>
              </tr>
            )}
            {items.map((it) => (
              <tr key={it.$id} className="border-t border-[var(--border)]">
                <td className="px-4 py-3 text-[var(--muted)]">{it.order ?? 0}</td>
                <td className="px-4 py-3 font-medium text-[var(--navy)]">{it.title}</td>
                <td className="px-4 py-3 text-[var(--muted)]">{it.slug}</td>
                <td className="px-4 py-3">
                  {it.featured && (
                    <span className="rounded-full bg-[var(--sky-50)] px-2 py-0.5 text-xs text-[var(--sky-600)]">
                      Öne çıkan
                    </span>
                  )}
                </td>
                <td className="px-4 py-3">
                  <div className="flex items-center justify-end gap-2">
                    <Link
                      href={`/sektor/${it.slug}`}
                      target="_blank"
                      className="rounded-md border border-[var(--border)] p-1.5 text-[var(--muted)] hover:text-[var(--navy)]"
                    >
                      <ExternalLink className="size-3.5" />
                    </Link>
                    <Link
                      href={`/admin/sektorler/${it.$id}/edit`}
                      className="inline-flex items-center gap-1 rounded-md border border-[var(--border)] bg-white px-2.5 py-1.5 text-xs font-medium text-[var(--navy)] hover:bg-[var(--navy-50)]"
                    >
                      <Edit className="size-3.5" /> Düzenle
                    </Link>
                    <form action={deleteIndustry}>
                      <input type="hidden" name="id" value={it.$id} />
                      <DeleteButton />
                    </form>
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
