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

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

  return (
    <div className="stack">
      <PageHeader title="Payments" eyebrow="Payment intents" />
      <div className="card">
        {payments.length ? (
          <div className="table-wrap">
            <table className="table">
              <thead><tr><th>ID</th><th>User</th><th>Status</th><th>Provider</th><th>Amount</th></tr></thead>
              <tbody>
                {payments.map((payment) => (
                  <tr key={payment.id}>
                    <td className="mono">{payment.id}</td>
                    <td className="mono">{payment.userId}</td>
                    <td><StatusBadge value={payment.status} /></td>
                    <td>{payment.providerKey}</td>
                    <td>{formatMoney(payment.amount, 'en')}</td>
                  </tr>
                ))}
              </tbody>
            </table>
          </div>
        ) : (
          <EmptyState title="No payment intents yet" body="Payment intents created by the API will appear here. No sample rows are invented." />
        )}
      </div>
    </div>
  );
}
