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

export default async function ProjectsAdminPage() {
  const projects = await listProjects();
  return (
    <div>
      <PageHeader
        title="Projeler"
        description="Portföyünüzdeki çalışmaları yönetin."
        action={
          <Link
            href="/admin/projeler/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 proje
          </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">Başlık</th>
              <th className="px-4 py-3 text-left">Kategori</th>
              <th className="px-4 py-3 text-left">Yıl</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>
            {projects.length === 0 && (
              <tr>
                <td colSpan={5} className="px-4 py-12 text-center text-[var(--muted)]">
                  Henüz proje yok.
                </td>
              </tr>
            )}
            {projects.map((p) => (
              <tr key={p.$id} className="border-t border-[var(--border)]">
                <td className="px-4 py-3 font-medium text-[var(--navy)]">{p.title}</td>
                <td className="px-4 py-3 text-[var(--muted)]">{p.category ?? "—"}</td>
                <td className="px-4 py-3 text-[var(--muted)]">{p.year ?? "—"}</td>
                <td className="px-4 py-3">
                  {p.featured ? (
                    <span className="rounded-full bg-[var(--sky-50)] px-2 py-0.5 text-xs text-[var(--sky-600)]">
                      Öne çıkan
                    </span>
                  ) : (
                    <span className="text-xs text-[var(--muted)]">—</span>
                  )}
                </td>
                <td className="px-4 py-3">
                  <div className="flex items-center justify-end gap-2">
                    {p.live_url && (
                      <a
                        href={p.live_url}
                        target="_blank"
                        rel="noopener"
                        className="rounded-md border border-[var(--border)] p-1.5 text-[var(--muted)] hover:text-[var(--navy)]"
                      >
                        <ExternalLink className="size-3.5" />
                      </a>
                    )}
                    <Link
                      href={`/admin/projeler/${p.$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={deleteProject}>
                      <input type="hidden" name="id" value={p.$id} />
                      <DeleteButton />
                    </form>
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
