vc-cli 0.1.0

Command-line interface for Visual Compiler
use anyhow::Result;
use clap::{Parser, Subcommand};
use std::fs;
use std::path::{Path, PathBuf};

use vc_adapter_sdxl::SdxlAdapter;
use vc_output::{CompilerOutput, SCHEMA_VERSION};
use vc_prompt::compile;
use vc_resolver::resolve;
use vc_runtime::{load_config, run_backend, RuntimeInput};
use vc_types::{AdapterKind, CompilerMetadata, CompilerVersion};

// Import fungsi hash dari crate yang sudah di-rename
use vc_bundle::bundle_hash;
use vc_prompt::prompt_hash;
use vc_snapshot::snapshot_hash;

mod snapshot_builder;
use snapshot_builder::minimal_snapshot;

#[derive(Parser)]
#[command(name = "vc", version = "0.1.0", about = "Visual Compiler")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    Demo {
        #[arg(short, long, default_value = "build")]
        out: PathBuf,
    },
    Run {
        #[arg(short, long, default_value = "build")]
        input: PathBuf,

        #[arg(short, long, default_value = "run")]
        out: PathBuf,

        /// Path to the configuration file (TOML).
        #[arg(short, long, default_value = "config.toml")]
        config: PathBuf,

        /// Runtime profile to use.
        #[arg(short, long, default_value = "default")]
        profile: String,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    match cli.command {
        Commands::Demo { out } => cmd_demo(&out)?,
        Commands::Run { input, out, config, profile } => cmd_run(&input, &out, &config, &profile)?,
    }
    Ok(())
}

fn cmd_demo(out_dir: &Path) -> Result<()> {
    println!("=== Visual Compiler Demo ===\n");
    fs::create_dir_all(out_dir)?;

    let snapshot = minimal_snapshot();
    let bundle = resolve(&snapshot);
    let prompt = compile(&bundle);

    let snapshot_hash = snapshot_hash(&snapshot)?;
    let bundle_hash = bundle_hash(&bundle)?;
    let prompt_hash = prompt_hash(&prompt)?;

    let version = CompilerVersion::parse(env!("CARGO_PKG_VERSION"))
        .unwrap_or_else(|_| CompilerVersion::new(0, 0, 0));

    let output = CompilerOutput {
        schema_version: SCHEMA_VERSION,
        prompt: prompt.clone(),
        metadata: CompilerMetadata {
            compiler_version: version,
            adapter: AdapterKind::Sdxl,
            snapshot_hash,
            bundle_hash,
            prompt_hash,
        },
    };

    let output_path = out_dir.join("compiler_output.json");
    fs::write(&output_path, serde_json::to_string_pretty(&output)?)?;
    println!("✅ Written compiler output to: {}", output_path.display());

    let rendered = SdxlAdapter::render(&prompt);
    let txt_path = out_dir.join("prompt.txt");
    fs::write(&txt_path, rendered)?;
    println!("✅ Written prompt text to: {}", txt_path.display());

    println!("\n✅ Demo complete.");
    Ok(())
}

fn cmd_run(input_dir: &Path, out_dir: &Path, config_path: &Path, profile_name: &str) -> Result<()> {
    fs::create_dir_all(out_dir)?;

    // 1. Load compiler output
    let output_path = input_dir.join("compiler_output.json");
    let json = fs::read_to_string(&output_path)?;
    let compiler_output: CompilerOutput = serde_json::from_str(&json)?;

    // 2. Field name changed to `vc_output`
    let input = RuntimeInput { vc_output: compiler_output };

    // 3. Load configuration
    let config = load_config(config_path)?;

    // 4. Run the backend
    let result = run_backend(&config, profile_name, &input, out_dir)?;

    // 5. Write runtime result
    let runtime_path = out_dir.join("runtime.json");
    fs::write(&runtime_path, serde_json::to_string_pretty(&result)?)?;
    println!("✅ Written runtime result to: {}", runtime_path.display());

    Ok(())
}