use anyhow::Result;
use clap::{Args, Parser};
use toast_api::{unified_cli, server, agent_cli};

#[derive(Parser, Debug)]
#[clap(author, version, about = "LLM API client and server (Claude and DeepSeek)")]
struct CliArgs {
    /// Run as server instead of CLI
    #[clap(short, long)]
    serve: bool,

    /// Use DeepSeek instead of Claude (default is Claude)
    #[clap(short = 'd', long)]
    deepseek: bool,

    /// Use Opus model variant
    #[clap(short = 'o', long, conflicts_with = "haiku")]
    opus: bool,

    /// Use Haiku model variant
    #[clap(short = 'k', long, conflicts_with = "opus")]
    haiku: bool,

    /// Use agent mode with DGM-inspired tool system
    #[clap(short = 'a', long)]
    agent: bool,

    // Server options (passed through when --serve is used)
    #[clap(flatten)]
    server_args: ServerArgs,
}

// Server-specific arguments
#[derive(Args, Debug, Clone)]
struct ServerArgs {
    /// Port to listen on (server mode only)
    #[clap(long, default_value = "3000")]
    port: u16,

    /// Default model to use (server mode only)
    #[clap(long, default_value = "claude-sonnet-4-claude-ai")]
    model: String,

    /// Host address to bind to (server mode only)
    #[clap(long, default_value = "0.0.0.0")]
    host: String,

    /// Enable debug logging (server mode only)
    #[clap(long)]
    debug: bool,
}

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize logging
    env_logger::init();
    
    let args = CliArgs::parse();

    if args.serve {
        // Convert our ServerArgs to server::Args
        let server_args = server::Args {
            port: args.server_args.port,
            model: args.server_args.model,
            host: args.server_args.host,
            debug: args.server_args.debug,
        };
        server::run_with_args(server_args).await
    } else if args.agent {
        // Run the agent-based CLI
        agent_cli::run_agent_cli(
            args.deepseek,
            args.opus,
            args.haiku,
        ).await
    } else {
        // Run the unified CLI with the appropriate provider and model options
        let cli_args = unified_cli::UnifiedArgs {
            use_deepseek: args.deepseek,
            use_opus: args.opus,
            use_haiku: args.haiku,
        };
        unified_cli::run(cli_args).await
    }
}
