"""Datastores CLI commands."""

from __future__ import annotations

import typer

datastores_app = typer.Typer(help="Manage datastores")


@datastores_app.command("list")
def datastores_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 datastores for an app."""
    from appliku.cli import get_client, render_output

    client = get_client()
    result = client.datastores.list(team, app_id)
    rows = [
        {"id": d.id, "name": d.name, "engine": d.engine, "version": d.version, "status": d.status}
        for d in result.results
    ]
    render_output(rows, ["id", "name", "engine", "version", "status"], output, title="Datastores")


@datastores_app.command("create")
def datastores_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="Datastore name"),
    engine: str = typer.Option(..., "--engine", "-e", help="Engine: postgres|mysql|redis|mongodb"),
    version: str | None = typer.Option(None, "--version", "-v", help="Engine version"),
) -> None:
    """Create a datastore."""
    from appliku.cli import get_client

    client = get_client()
    kwargs: dict[str, object] = {}
    if version:
        kwargs["version"] = version
    ds = client.datastores.create(team, app_id, name, engine, **kwargs)
    typer.echo(f"Created datastore '{ds.name}' (ID: {ds.id}, engine: {ds.engine})")


@datastores_app.command("delete")
def datastores_delete(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    ds_id: int = typer.Argument(..., help="Datastore ID"),
) -> None:
    """Delete a datastore."""
    from appliku.cli import get_client

    client = get_client()
    client.datastores.delete(team, app_id, ds_id)
    typer.echo(f"Deleted datastore {ds_id}")


@datastores_app.command("start")
def datastores_start(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    ds_id: int = typer.Argument(..., help="Datastore ID"),
) -> None:
    """Start a datastore."""
    from appliku.cli import get_client

    client = get_client()
    ds = client.datastores.start(team, app_id, ds_id)
    typer.echo(f"Started datastore '{ds.name}' (status: {ds.status})")


@datastores_app.command("stop")
def datastores_stop(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    ds_id: int = typer.Argument(..., help="Datastore ID"),
) -> None:
    """Stop a datastore."""
    from appliku.cli import get_client

    client = get_client()
    ds = client.datastores.stop(team, app_id, ds_id)
    typer.echo(f"Stopped datastore '{ds.name}' (status: {ds.status})")


@datastores_app.command("restart")
def datastores_restart(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    ds_id: int = typer.Argument(..., help="Datastore ID"),
) -> None:
    """Restart a datastore."""
    from appliku.cli import get_client

    client = get_client()
    ds = client.datastores.restart(team, app_id, ds_id)
    typer.echo(f"Restarted datastore '{ds.name}' (status: {ds.status})")


@datastores_app.command("logs")
def datastores_logs(
    team: str = typer.Option(..., "--team", "-t", help="Team path"),
    app_id: int = typer.Option(..., "--app", "-a", help="App ID"),
    ds_id: int = typer.Argument(..., help="Datastore ID"),
) -> None:
    """View logs for a datastore."""
    from rich.console import Console

    from appliku.cli import get_client

    client = get_client()
    with Console().status("Fetching datastore logs..."):
        result = client.datastores.logs(team, app_id, ds_id)
    typer.echo(result.logs)
