import { LogIn, LogOut, Clock } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Auth_get_user } from "@/lib/auth"
import { Auth_tinyauth_public_endpoint } from "@/lib/auth_shared"
import Link from "next/link"

export async function AuthButtons() {
  const user = await Auth_get_user()

  if (user.isLoggedIn) {
    return (
      <div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-3">
        <span className="text-sm text-muted-foreground">{user.email}</span>
        <div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-3">
          <Link href="/history" className="w-full sm:w-auto">
            <Button variant="outline" size="sm" className="w-full sm:w-auto">
              <Clock className="mr-2 h-4 w-4" />
              History
            </Button>
          </Link>
          <form action="/logout" method="post" className="w-full sm:w-auto">
            <Button type="submit" variant="outline" className="w-full sm:w-auto">
              <LogOut className="mr-2 h-4 w-4" />
              Logout
            </Button>
          </form>
        </div>
      </div>
    )
  }

  const authUrl = `${Auth_tinyauth_public_endpoint()}/login`

  return (
    <div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-3">
      <Link href="/history" className="w-full sm:w-auto">
        <Button variant="outline" size="sm" className="w-full sm:w-auto">
          <Clock className="mr-2 h-4 w-4" />
          History
        </Button>
      </Link>
      <Button asChild className="w-full sm:w-auto">
        <a href={authUrl}>
          <LogIn className="mr-2 h-4 w-4" />
          Login
        </a>
      </Button>
    </div>
  )
}
