"""Domains CLI commands."""

from __future__ import annotations

import typer

domains_app = typer.Typer(help="Manage domains")


@domains_app.command("list")
def domains_list(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    output: str = typer.Option("table", "--output", "-o", help="Output format"),
) -> None:
    """List domains for an app."""
    from appliku.cli import get_client, render_output

    client = get_client()
    result = client.domains.list(team, app_id)
    rows = [
        {"id": d.id, "domain": d.domain, "deployment_status": d.deployment_status}
        for d in result.results
    ]
    render_output(rows, ["id", "domain", "deployment_status"], output, title="Domains")


@domains_app.command("create")
def domains_create(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    domain: str = typer.Argument(..., help="Domain name"),
) -> None:
    """Add a domain to an app."""
    from appliku.cli import get_client

    client = get_client()
    d = client.domains.create(team, app_id, domain)
    typer.echo(f"Added domain '{d.domain}' (ID: {d.id})")


@domains_app.command("delete")
def domains_delete(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    domain_id: int = typer.Argument(..., help="Domain ID"),
) -> None:
    """Remove a domain from an app."""
    from appliku.cli import get_client

    client = get_client()
    client.domains.delete(team, app_id, domain_id)
    typer.echo(f"Deleted domain {domain_id}")
