import Link from "next/link";
import { Plus, Pencil, MapIcon } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { DeleteButton } from "@/components/admin/DeleteButton";
import { EmptyState } from "@/components/ui/EmptyState";
import { deleteTour } from "./actions";

export default async function AdminToursPage() {
  const tours = await prisma.tour.findMany({
    orderBy: [{ isFeatured: "desc" }, { sortOrder: "asc" }],
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-semibold text-heading">Paket Tour</h1>
          <p className="mt-1 text-sm text-body">Kelola daftar paket tour.</p>
        </div>
        <Link
          href="/admin/tours/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 Tour
        </Link>
      </div>

      <div className="mt-6 overflow-hidden rounded-2xl bg-white shadow-sm">
        {tours.length === 0 ? (
          <EmptyState message="Belum ada paket tour." icon={MapIcon} />
        ) : (
          <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">Judul</th>
                  <th className="px-6 py-3">Lokasi</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">
                {tours.map((tour) => (
                  <tr key={tour.id}>
                    <td className="px-6 py-4 font-medium text-heading">
                      {tour.title}
                    </td>
                    <td className="px-6 py-4 text-body">{tour.location}</td>
                    <td className="px-6 py-4 text-body">{tour.price}</td>
                    <td className="px-6 py-4">
                      {tour.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/tours/${tour.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={deleteTour.bind(null, tour.id)}
                          confirmMessage={`Hapus tour "${tour.title}"?`}
                        />
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </div>
  );
}
