"""Invites CLI commands."""

from __future__ import annotations

import typer

invites_app = typer.Typer(help="Manage team invites")


@invites_app.command("list")
def invites_list(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    output: str = typer.Option("table", "--output", "-o", help="Output format"),
) -> None:
    """List invites for a team."""
    from appliku.cli import get_client, render_output

    client = get_client()
    result = client.invites.list(team)
    rows = [
        {
            "id": i.id,
            "email": i.email,
            "role": i.role,
            "status": i.status,
            "created_at": i.created_at,
        }
        for i in result.results
    ]
    render_output(rows, ["id", "email", "role", "status", "created_at"], output, title="Invites")


@invites_app.command("create")
def invites_create(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    email: str = typer.Option(..., "--email", "-e", help="Email to invite"),
    role: str = typer.Option("member", "--role", "-r", help="Role: member|admin"),
) -> None:
    """Create an invite."""
    from appliku.cli import get_client

    client = get_client()
    invite = client.invites.create(team, email, role)
    typer.echo(f"Invited '{invite.email}' (ID: {invite.id}, role: {invite.role})")


@invites_app.command("delete")
def invites_delete(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    invite_id: int = typer.Argument(..., help="Invite ID"),
) -> None:
    """Delete/revoke an invite."""
    from appliku.cli import get_client

    client = get_client()
    client.invites.delete(team, invite_id)
    typer.echo(f"Deleted invite {invite_id}")


@invites_app.command("accept")
def invites_accept(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    invite_id: int = typer.Option(..., "--id", "-i", help="Invite ID"),
    token: str = typer.Option(..., "--token", "-k", help="Invite token"),
) -> None:
    """Accept an invite."""
    from appliku.cli import get_client

    client = get_client()
    client.invites.accept(team, invite_id, token)
    typer.echo(f"Accepted invite {invite_id}")


@invites_app.command("decline")
def invites_decline(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    invite_id: int = typer.Option(..., "--id", "-i", help="Invite ID"),
    token: str = typer.Option(..., "--token", "-k", help="Invite token"),
) -> None:
    """Decline an invite."""
    from appliku.cli import get_client

    client = get_client()
    client.invites.decline(team, invite_id, token)
    typer.echo(f"Declined invite {invite_id}")
