import type { Metadata } from 'next';
import Link from 'next/link';
import { redirect } from 'next/navigation';
import { LoginForm } from '@/components/host/LoginForm';
import { LoginShell } from '@/components/host/LoginShell';
import { linkStyles } from '@/components/host/ui';
import { safeNextPath } from '@/lib/auth/cookies';
import { getHostContext } from '@/lib/auth/session';
import { signInHost } from './actions';

export const metadata: Metadata = { title: 'Sign in' };

export default async function HostLoginPage({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {
  const { next } = await searchParams;
  const destination = safeNextPath('host', typeof next === 'string' ? next : undefined);
  // Already signed in: straight to the dashboard.
  if (await getHostContext()) redirect(destination);
  return (
    <LoginShell
      kicker="Venue hosts"
      title="Sign in to your bar"
      intro="Run tonight’s game, schedule championships and see who keeps coming back."
      footer={
        <>
          New to PintRush?{' '}
          <Link href="/pilot" prefetch={false} className={linkStyles}>
            Apply for the venue pilot
          </Link>
          . Forgot your password? Ask your venue owner or PintRush support to reset it.
        </>
      }
    >
      <LoginForm action={signInHost} next={destination === '/host' ? undefined : destination} submitLabel="Sign in" />
    </LoginShell>
  );
}
