import { redirect } from 'next/navigation'
import { getProfile, getSessionUser } from '@/lib/auth'
import { getVisibleTrips } from '@/lib/data/trips'
import { AppShell } from '@/components/app/app-shell'
import { BackLink } from '@/components/app/back-link'
import { AlertsList } from './alerts-list'

const SEV_ORDER = { danger: 0, warning: 1, info: 2 } as const

/** Alerts — every active warning across the user's trips, most severe first. */
export default async function AlertsPage() {
  const user = await getSessionUser()
  if (!user) redirect('/login')
  const profile = (await getProfile())!

  const { trips, warnings } = await getVisibleTrips(user)
  const sorted = [...warnings].sort((a, b) => SEV_ORDER[a.severity] - SEV_ORDER[b.severity])

  return (
    <AppShell profile={profile}>
      <main className="mx-auto max-w-3xl px-4 py-8">
        <BackLink />
        <h1 className="mt-3 text-2xl font-bold tracking-tight">Alerts</h1>
        <p className="mt-1 text-sm text-neutral-500">
          Every active warning across your trips — curfews, escorts, dimensions, and expirations.
          Acknowledge one (✓) to hide it for you; others still see it.
        </p>

        <div className="mt-6">
          <AlertsList warnings={sorted} trips={trips} userId={user.id} />
        </div>

        <p className="mt-8 text-center text-xs text-neutral-400">
          Location-aware alerts (current and upcoming states) arrive with driver location — coming
          soon.
        </p>
      </main>
    </AppShell>
  )
}
