import Link from "next/link"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle, CardFooter } from "@/components/ui/card"
import { auth, signIn } from "@/auth"
import { redirect } from "next/navigation";
import { SiAuthentik } from "react-icons/si"
import { UserPlus } from "lucide-react"

export default async function Login() {
  const session = await auth()
  
  if (session) {
    return redirect("/account/dashboard")
  }
  
  return (
    <div className="flex h-screen items-center justify-center">
      <Card className="w-full max-w-md">
        <CardHeader>
          <CardTitle className="text-4xl text-center">LibreCloud</CardTitle>
        </CardHeader>
        <CardContent>
          <form
            className="space-y-4"
            action={async () => {
              "use server"
              await signIn("authentik", { redirectTo: "/account/dashboard" })
            }}
          >
            <Button type="submit" className="w-full">
              <SiAuthentik />
              Sign in with Authentik
            </Button>
            {process.env.SIGNUP_ENABLED === "true" ? (
              <Link href="/account/signup">
                <Button variant="outline" className="w-full">
                  <UserPlus />
                  Create an Account
                </Button>
              </Link>
            ) : (
              <Button variant="outline" className="w-full cursor-not-allowed" disabled>
                <UserPlus />
                Registration is Closed
              </Button>
            )}
          </form>
        </CardContent>
        <CardFooter className="justify-center">
          <p className="text-sm text-muted-foreground -mt-3">By continuing, you agree to our <Link href="/privacy" className="underline">Privacy Policy</Link>.</p>
        </CardFooter>
      </Card>
    </div>
  )
}

