/// DreamStack CLI — the compiler command-line interface.
///
/// Usage:
///   dreamstack build <file.ds>   — compile to HTML+JS
///   dreamstack dev <file.ds>     — dev server with hot reload
///   dreamstack check <file.ds>   — analyze without emitting

use clap::{Parser, Subcommand};
use std::path::PathBuf;

mod commands;

#[derive(Parser)]
#[command(name = "dreamstack")]
#[command(about = "The DreamStack UI compiler", version = "0.6.0")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Compile a .ds file to HTML+JS or Panel IR
    Build {
        /// Input .ds file
        file: PathBuf,
        /// Output directory (default: dist/)
        #[arg(short, long, default_value = "dist")]
        output: PathBuf,
        /// Minify JS and CSS output
        #[arg(long)]
        minify: bool,
        /// Target: html (default) or panel (ESP32 LVGL IR)
        #[arg(long, default_value = "html")]
        target: String,
    },
    /// Start a dev server with hot reload
    Dev {
        /// Input .ds file
        file: PathBuf,
        /// Port to serve on
        #[arg(short, long, default_value_t = 3000)]
        port: u16,
    },
    /// Type-check and analyze without compiling
    Check {
        /// Input .ds file
        file: PathBuf,
    },
    /// Compile and stream a .ds file via bitstream relay
    Stream {
        /// Input .ds file
        file: PathBuf,
        /// WebSocket relay URL
        #[arg(short, long, default_value = "ws://localhost:9100")]
        relay: String,
        /// Stream mode: pixel | delta | signal
        #[arg(short, long, default_value = "signal")]
        mode: String,
        /// Port to serve the source page on
        #[arg(short, long, default_value_t = 3000)]
        port: u16,
    },
    /// Launch the interactive playground with Monaco editor
    Playground {
        /// Optional .ds file to pre-load
        file: Option<PathBuf>,
        /// Port to serve on
        #[arg(short, long, default_value_t = 4000)]
        port: u16,
    },
    /// Add a component from the DreamStack registry
    Add {
        /// Component name (e.g., button, card, dialog) or --list to show available
        name: Option<String>,
        /// List all available components
        #[arg(long)]
        list: bool,
        /// Add all registry components
        #[arg(long)]
        all: bool,
    },
    /// Convert a React/TSX component to DreamStack .ds format
    Convert {
        /// Input .tsx file path, or component name with --shadcn
        name: String,
        /// Fetch from shadcn/ui registry instead of local file
        #[arg(long)]
        shadcn: bool,
        /// Output file path (default: stdout)
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
    /// Initialize a new DreamStack project
    Init {
        /// Project name (creates directory; default: current dir)
        name: Option<String>,
    },
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Build { file, output, minify, target } => commands::build::cmd_build(&file, &output, minify, &target),
        Commands::Dev { file, port } => commands::dev::cmd_dev(&file, port),
        Commands::Check { file } => commands::check::cmd_check(&file),
        Commands::Stream { file, relay, mode, port } => commands::stream::cmd_stream(&file, &relay, &mode, port),
        Commands::Playground { file, port } => commands::playground::cmd_playground(file.as_deref(), port),
        Commands::Add { name, list, all } => commands::add::cmd_add(name, list, all),
        Commands::Convert { name, shadcn, output } => commands::convert::cmd_convert(&name, shadcn, output.as_deref()),
        Commands::Init { name } => commands::init::cmd_init(name),
    }
}
