use anyhow::Error;
use clap::Parser;
use log::info;

mod cli;

use syrac_score::{
    flight::Flight,
    geo::ConvertibleMetric,
    score::{score_flight_on_task, TaskResult},
    task::Task,
};

use crate::cli::Args;

fn main() -> Result<(), Error> {
    env_logger::init();

    let args = Args::parse();
    let flight = Flight::from_igc_file(&args.flight).map_err(|e| Error::msg(e.to_string()))?;
    info!("{flight}");

    let task = Task::from_xctsk_file(&args.task).map_err(|e| Error::msg(e.to_string()))?;
    let route = task.find_shortest_route()?;
    info!(
        "{:.2}km ({:.2}km) {}",
        route.total_distance().to_km(),
        route.speed_section.distance.to_km(),
        task
    );

    let result = score_flight_on_task(&task, &flight);
    match result {
        TaskResult::NotCompleted => info!("did not complete task"),
        TaskResult::Completed(attempt) => {
            info!(
                "completed attempt in {} ({:.2}km/h)",
                attempt.elapsed_time(&task)?,
                attempt.mean_speed(&task, &route)?
            )
        }
    }
    Ok(())
}
