import Link from "next/link";
import { Plus, Pencil, Plane } from "lucide-react";
import { prisma } from "@/lib/prisma";
import { DeleteButton } from "@/components/admin/DeleteButton";
import { EmptyState } from "@/components/ui/EmptyState";
import { deleteFlight } from "./actions";

export default async function AdminFlightsPage() {
  const flights = await prisma.flight.findMany({
    orderBy: [{ isFeatured: "desc" }, { sortOrder: "asc" }],
  });

  return (
    <div>
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-semibold text-heading">
            Tiket Pesawat
          </h1>
          <p className="mt-1 text-sm text-body">
            Kelola daftar tiket pesawat.
          </p>
        </div>
        <Link
          href="/admin/flights/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 Penerbangan
        </Link>
      </div>

      <div className="mt-6 overflow-hidden rounded-2xl bg-white shadow-sm">
        {flights.length === 0 ? (
          <EmptyState message="Belum ada tiket pesawat." icon={Plane} />
        ) : (
          <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">Maskapai</th>
                  <th className="px-6 py-3">Rute</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">
                {flights.map((flight) => (
                  <tr key={flight.id}>
                    <td className="px-6 py-4 font-medium text-heading">
                      {flight.airline}
                      {flight.flightNumber ? ` · ${flight.flightNumber}` : ""}
                    </td>
                    <td className="px-6 py-4 text-body">
                      {flight.origin} &rarr; {flight.destination}
                    </td>
                    <td className="px-6 py-4 text-body">{flight.price}</td>
                    <td className="px-6 py-4">
                      {flight.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/flights/${flight.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={deleteFlight.bind(null, flight.id)}
                          confirmMessage={`Hapus penerbangan "${flight.airline}"?`}
                        />
                      </div>
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        )}
      </div>
    </div>
  );
}
