"""
CLI helper for API execution mode.

Provides reusable helper to route CLI commands to API execution.
Follows CLAUDE.md: Single responsibility, <50 lines/method.
"""

from pathlib import Path
from typing import Optional, Dict, Any
import typer
from rich.console import Console

from .router import ExecutionRouter


def execute_stage_via_api(
    job_type: str,
    config_path: Path,
    dataset_name: Optional[str] = None,
    description: Optional[str] = None,
    dry_run: bool = False,
    **kwargs,
) -> None:
    """
    Execute stage job via API.

    Single responsibility: Route stage command to API execution.
    Method size: <50 lines (CLAUDE.md compliant).

    Args:
        job_type: Job type (generate, k8s, metadata, usage, discount, upload)
        config_path: Path to config file or directory
        dataset_name: Optional dataset name
        description: Optional job description
        dry_run: Dry run flag (not supported in API mode)
        **kwargs: Additional job parameters
    """
    console = Console()

    # Dry run not supported in API mode
    if dry_run:
        console.print("[yellow]⚠️  Dry run is not supported in API mode.[/yellow]")
        console.print("[yellow]   Please use --local for dry run analysis.[/yellow]")
        raise typer.Exit(1)

    try:
        # Initialize execution router
        router = ExecutionRouter(console)

        # Execute via API
        router.execute_api(
            job_type=job_type,
            config_path=config_path,
            dataset_name=dataset_name,
            description=description or f"{job_type} job",
            **kwargs,
        )

    except Exception as e:
        console.print(f"[red]✗ API execution failed: {e}[/red]")
        raise typer.Exit(1)
