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

export default async function SolutionsAdminPage() {
  const solutions = await listSolutions();
  return (
    <div>
      <PageHeader
        title="Çözümler"
        description="Anasayfa ve /cozumler sayfasında gösterilen çözüm kartları."
        action={
          <Link
            href="/admin/cozumler/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 çözüm
          </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">İkon</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>
            {solutions.length === 0 && (
              <tr>
                <td colSpan={6} className="px-4 py-12 text-center text-[var(--muted)]">
                  Çözüm eklenmemiş.
                </td>
              </tr>
            )}
            {solutions.map((s) => (
              <tr key={s.$id} className="border-t border-[var(--border)]">
                <td className="px-4 py-3 text-[var(--muted)]">{s.order ?? 0}</td>
                <td className="px-4 py-3 font-medium text-[var(--navy)]">{s.title}</td>
                <td className="px-4 py-3 text-[var(--muted)]">{s.slug}</td>
                <td className="px-4 py-3 text-[var(--muted)]">{s.icon ?? "—"}</td>
                <td className="px-4 py-3">
                  {s.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">
                    <Link
                      href={`/admin/cozumler/${s.$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={deleteSolution}>
                      <input type="hidden" name="id" value={s.$id} />
                      <DeleteButton />
                    </form>
                  </div>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </div>
    </div>
  );
}
