import { AppShell } from '@/components/app/app-shell'
import { requireRoleRoute } from '@/lib/role-route'
import { loadBrokerPage } from '@/lib/data/dashboard'
import { loadBrokerDispatchers } from '@/lib/data/contacts'
import { BrokerRequestForm } from './broker-request-form'

/**
 * Freight Broker · New Trip Request — `/fb-new-trip-request` (Nash,
 * 2026-09-13). Drag & drop the rate confirmation, pick a dispatcher from the
 * saved contacts or type name / email / phone, then choose: the carrier
 * uploads its own permits, or request permits from Synchron Permits (with
 * who pays). The shared workspace starts as "Waiting on Permits".
 */
export default async function FreightBrokerNewTripRequestPage() {
  const { user, profile } = await requireRoleRoute('broker')
  const [page, dispatchers] = await Promise.all([loadBrokerPage(user), loadBrokerDispatchers(user)])
  return (
    <AppShell profile={profile}>
      <main className="mx-auto max-w-2xl px-4 py-8">
        <h1 className="text-2xl font-bold tracking-tight">Create a trip request</h1>
        <p className="mt-1 text-sm text-neutral-600">
          Upload the rate confirmation, invite the dispatcher, and decide how the permits get
          handled — the shared workspace starts as &ldquo;Waiting on Permits&rdquo;.
        </p>
        {page ? (
          <BrokerRequestForm
            slug={page.slug}
            defaultPolicy={page.default_permit_policy ?? 'ask'}
            defaultPayment={page.default_payment_party ?? 'ask'}
            dispatchers={dispatchers}
          />
        ) : (
          <p className="mt-6 rounded-xl border border-dashed p-8 text-center text-sm text-neutral-500">
            Your broker intake page could not be created, so the request form cannot open. Open the
            freight broker dashboard once (it creates the page), then come back.
          </p>
        )}
      </main>
    </AppShell>
  )
}
