use std::fs::File;

use clap::Parser;
use log::LevelFilter;
use oo_ide::{app, log_buffer, project};

#[derive(Parser, Debug)]
#[command(name = "clyde", version, about = "A terminal IDE")]
struct Cli {
    #[arg(default_value = ".")]
    project: std::path::PathBuf,
}

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();

    let project_dir = cli.project.join(".oo");
    if !project_dir.exists() {
        std::fs::create_dir(&project_dir)?;
    }
    let cache_dir = project_dir.join("cache");
    std::fs::create_dir_all(&cache_dir)?;

    let file = File::create(cache_dir.join("app.log")).unwrap();
    let log_state = log_buffer::init_combined_logger(file, LevelFilter::Debug);

    let mut project = project::Project::new(cli.project)?;
    // Restore small per-project cached state (.oo/project_state.yaml)
    project.restore_state();
    app::run(project, log_state)
}
