"use client"

import { useSearchParams } from "next/navigation"
import { Suspense } from "react"
import { LoginForm } from "@/components/auth/login-form"
import { useNativeRuntime } from "@/lib/platform"
import { NativeLogin } from "@/components/mobile/native-login"

function LoginContent() {
  const searchParams = useSearchParams()
  const error = searchParams.get("error")
  const returnTo = searchParams.get("returnTo") ?? "/mail/inbox"
  const loginHref = `/api/auth/login?returnTo=${encodeURIComponent(returnTo)}`
  const signupHref = `/signup?returnTo=${encodeURIComponent(returnTo)}`
  const forgotPasswordHref = `/forgot-password?returnTo=${encodeURIComponent(returnTo)}`

  if (useNativeRuntime()) {
    return <NativeLogin returnTo={returnTo} />
  }

  return (
    <LoginForm
      loginHref={loginHref}
      signupHref={signupHref}
      forgotPasswordHref={forgotPasswordHref}
      error={error}
    />
  )
}

export default function LoginPage() {
  return (
    <Suspense fallback={null}>
      <LoginContent />
    </Suspense>
  )
}
