use deciduous::{
    CommandLog, DecisionContext, DecisionEdge, DecisionNode, DecisionSession, RoadmapConflict,
    RoadmapItem, RoadmapSyncState,
};
use std::fs;
use std::path::PathBuf;
use ts_rs::TS;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let output_dir = PathBuf::from("web/src/types/generated");
    if !output_dir.exists() {
        fs::create_dir_all(&output_dir)?;
    }

    // Generate TypeScript definitions
    let definitions = vec![
        ("DecisionNode", DecisionNode::decl()),
        ("DecisionEdge", DecisionEdge::decl()),
        ("DecisionContext", DecisionContext::decl()),
        ("DecisionSession", DecisionSession::decl()),
        ("CommandLog", CommandLog::decl()),
        ("RoadmapItem", RoadmapItem::decl()),
        ("RoadmapSyncState", RoadmapSyncState::decl()),
        ("RoadmapConflict", RoadmapConflict::decl()),
    ];

    let mut content = String::from(
        "// This file is auto-generated by bin/gen_types.rs\n// Do not edit manually.\n\n",
    );

    for (_name, decl) in definitions {
        content.push_str(&format!("export {}\n\n", decl));
    }

    let output_file = output_dir.join("schema.ts");
    fs::write(&output_file, content)?;

    println!("Generated TypeScript definitions at {:?}", output_file);
    Ok(())
}
