import Link from "next/link";
import { Plus, Pencil, MapPin } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { DeleteButton } from "@/components/admin/DeleteButton";
import { EmptyState } from "@/components/ui/EmptyState";
import { deleteDestination } from "./actions";

export default async function AdminDestinationsPage() {
  const destinations = await prisma.destination.findMany({
    orderBy: [{ isFeatured: "desc" }, { sortOrder: "asc" }],
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-semibold text-heading">Destinasi Populer</h1>
          <p className="mt-1 text-sm text-body">Kelola daftar destinasi populer di halaman Home.</p>
        </div>
        <Link
          href="/admin/destinations/create"
          className="flex items-center gap-2 rounded-full bg-merah-ranata px-4 py-2 text-sm font-semibold text-white transition hover:bg-merah-hover"
        >
          <Plus className="h-4 w-4" />
          Tambah Destinasi
        </Link>
      </div>

      <div className="mt-6 overflow-hidden rounded-2xl bg-white shadow-sm">
        {destinations.length === 0 ? (
          <EmptyState message="Belum ada destinasi." icon={MapPin} />
        ) : (
          <div className="overflow-x-auto">
            <table className="w-full min-w-[640px] text-left text-sm">
              <thead className="bg-bg-light text-xs uppercase text-muted">
                <tr>
                  <th className="px-6 py-3">Nama</th>
                  <th className="px-6 py-3">Durasi</th>
                  <th className="px-6 py-3">Harga</th>
                  <th className="px-6 py-3">Unggulan</th>
                  <th className="px-6 py-3 text-right">Aksi</th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-100">
                {destinations.map((dest) => (
                  <tr key={dest.id}>
                    <td className="px-6 py-4 font-medium text-heading">
                      {dest.name}
                      {dest.badge && (
                        <span className="ml-2 rounded-full bg-red-50 px-2 py-0.5 text-xs font-medium text-merah-ranata">
                          {dest.badge}
                        </span>
                      )}
                    </td>
                    <td className="px-6 py-4 text-body">{dest.duration || "-"}</td>
                    <td className="px-6 py-4 text-body">{dest.price || "-"}</td>
                    <td className="px-6 py-4">
                      {dest.isFeatured ? (
                        <span className="rounded-full bg-red-50 px-2 py-1 text-xs font-medium text-merah-ranata">
                          Unggulan
                        </span>
                      ) : (
                        <span className="text-muted">-</span>
                      )}
                    </td>
                    <td className="px-6 py-4">
                      <div className="flex items-center justify-end gap-2">
                        <Link
                          href={`/admin/destinations/${dest.id}/edit`}
                          className="rounded-lg p-2 text-gray-500 transition hover:bg-bg-light hover:text-heading"
                          aria-label="Edit"
                        >
                          <Pencil className="h-4 w-4" />
                        </Link>
                        <DeleteButton
                          action={deleteDestination.bind(null, dest.id)}
                          confirmMessage={`Hapus destinasi "${dest.name}"?`}
                        />
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </div>
  );
}
