import { notFound } from 'next/navigation'
import { eq } from 'drizzle-orm'
import { db, schema } from '@/db'
import { getStationId } from '@/lib/auth'
import PinForm from './PinForm'
import BarApp from './BarApp'
import CashierApp from './CashierApp'

export const dynamic = 'force-dynamic'
export const metadata = { title: 'Estación', robots: { index: false } }

export default async function StationPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params
  if (!/^[0-9a-f-]{36}$/.test(id)) notFound()
  const [row] = await db.select({ station: schema.stations, eventName: schema.events.name })
    .from(schema.stations).innerJoin(schema.events, eq(schema.events.id, schema.stations.eventId))
    .where(eq(schema.stations.id, id))
  if (!row) notFound()

  if ((await getStationId()) !== id) {
    return <PinForm stationId={id} stationName={row.station.name} eventName={row.eventName} />
  }
  return row.station.kind === 'bar' ? <BarApp stationId={id} /> : <CashierApp stationId={id} />
}
