import typer
from ..project import get_projects, get_services
from ..auth import auth


app = typer.Typer(help="Retrive information about your projects and services")


@app.command(help="List all projects")
def projects():
    try:
        user = auth()
        projects = get_projects(user)
        if len(projects) == 0:
            return typer.echo(f"No projects found.")
        typer.echo("Projects:")
        for project in projects:
            typer.echo(project)
    except Exception as e:
        return typer.echo(e)


@app.command(help="List all services in a project")
def services(
    project: str = typer.Argument(..., help="Project name"),
):
    try:
        user = auth()
        if project is None:
            raise Exception("Project name is required.")
        services = get_services(user, project)
        if len(services) == 0:
            return typer.echo(f"No services running in project '{project}'.")
        return typer.echo(f"Services in project '{project}': {services}")
    except Exception as e:
        return typer.echo(e)


if __name__ == "__main__":
    app()
