import type { Booking } from '@navi/types';
import { formatMoney } from '@navi/ui';
import { AccessDenied } from '../../../components/AccessDenied';
import { EmptyState, PageHeader, StatusBadge } from '../../../components/DashboardPrimitives';
import { dashboardApi } from '../../../lib/api';
import { requireDashboardRoute } from '../../../lib/permissions';

export default async function Bookings() {
  const gate = await requireDashboardRoute('/bookings');
  if (!gate.allowed) return <AccessDenied required={gate.required} />;
  const bookings = (await dashboardApi<Booking[]>('/bookings')) ?? [];

  return (
    <div className="stack">
      <PageHeader title="Bookings" eyebrow="Reservations" />
      <div className="card">
        {bookings.length ? (
          <div className="table-wrap">
            <table className="table">
              <thead><tr><th>ID</th><th>Kind</th><th>Status</th><th>Guests</th><th>Total</th></tr></thead>
              <tbody>
                {bookings.map((booking) => (
                  <tr key={booking.id}>
                    <td className="mono">{booking.id}</td>
                    <td>{booking.kind}</td>
                    <td><StatusBadge value={booking.status} /></td>
                    <td>{booking.guestCount}</td>
                    <td>{formatMoney(booking.total, 'en')}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        ) : (
          <EmptyState title="No bookings found" body="Bookings created through the mobile app or API will appear here within your permission scope." />
        )}
      </div>
    </div>
  );
}
