from typer import Typer, echo

from harbor.cli.utils import run_async

auth_app = Typer(no_args_is_help=True)


@auth_app.command()
def login() -> None:
    """Authenticate with Harbor via GitHub OAuth."""

    async def _login():
        from harbor.auth.handler import get_auth_handler

        handler = await get_auth_handler()
        user_id = await handler.login()
        echo(f"Logged in as {user_id}")
        echo(
            "Visit https://harbor-registry-website.vercel.app/profile to create and manage orgs."
        )

    run_async(_login())


@auth_app.command()
def logout() -> None:
    """Sign out and clear stored credentials."""

    async def _logout():
        from harbor.auth.handler import get_auth_handler

        handler = await get_auth_handler()
        await handler.logout()
        echo("Logged out")

    run_async(_logout())


@auth_app.command()
def status() -> None:
    """Show current authentication status."""

    async def _status():
        from harbor.auth.handler import get_auth_handler

        handler = await get_auth_handler()
        if not await handler.is_authenticated():
            echo("Not authenticated. Run `harbor auth login`.")
            return

        username = await handler.get_github_username()
        label = username or "unknown user"
        echo(f"Logged in as {label}")

    run_async(_status())
