"""CronJobs CLI commands."""

from __future__ import annotations

import typer

crons_app = typer.Typer(help="Manage cron jobs")


@crons_app.command("list")
def crons_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 cron jobs for an app."""
    from appliku.cli import get_client, render_output

    client = get_client()
    result = client.cron_jobs.list(team, app_id)
    rows = [
        {
            "id": c.id,
            "name": c.name,
            "schedule": c.schedule,
            "command": c.command,
            "is_disabled": c.is_disabled,
        }
        for c in result.results
    ]
    render_output(
        rows, ["id", "name", "schedule", "command", "is_disabled"], output, title="Cron Jobs"
    )


@crons_app.command("create")
def crons_create(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    name: str = typer.Option(..., "--name", "-n", help="Cron job name"),
    schedule: str = typer.Option(..., "--schedule", "-s", help="Cron schedule (e.g. '0 * * * *')"),
    command: str = typer.Option(..., "--command", "-c", help="Command to run"),
) -> None:
    """Create a cron job."""
    from appliku.cli import get_client

    client = get_client()
    cron = client.cron_jobs.create(team, app_id, name, schedule, command)
    typer.echo(f"Created cron job '{cron.name}' (ID: {cron.id})")


@crons_app.command("update")
def crons_update(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    cron_id: int = typer.Argument(..., help="Cron job ID"),
    name: str | None = typer.Option(None, "--name", "-n"),
    schedule: str | None = typer.Option(None, "--schedule", "-s"),
    command: str | None = typer.Option(None, "--command", "-c"),
    disabled: bool | None = typer.Option(None, "--disabled", help="Enable or disable"),
) -> None:
    """Update a cron job."""
    from appliku.cli import get_client

    client = get_client()
    kwargs: dict[str, object] = {}
    if name is not None:
        kwargs["name"] = name
    if schedule is not None:
        kwargs["schedule"] = schedule
    if command is not None:
        kwargs["command"] = command
    if disabled is not None:
        kwargs["is_disabled"] = disabled
    cron = client.cron_jobs.update(team, app_id, cron_id, **kwargs)
    typer.echo(f"Updated cron job '{cron.name}'")


@crons_app.command("delete")
def crons_delete(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    cron_id: int = typer.Argument(..., help="Cron job ID"),
) -> None:
    """Delete a cron job."""
    from appliku.cli import get_client

    client = get_client()
    client.cron_jobs.delete(team, app_id, cron_id)
    typer.echo(f"Deleted cron job {cron_id}")
