"use client"

import { useSearchParams } from "next/navigation"
import { Suspense } from "react"
import { LoginForm } from "@/components/auth/login-form"
import { getAuthentikEnrollmentUrl } from "@/lib/auth/oidc-config"
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 = getAuthentikEnrollmentUrl()

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

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

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