#![allow(clippy::expect_used, clippy::panic)]

use super::*;

const SAMPLE_FPL: &str = r#"<?xml version="1.0" encoding="utf-8"?>
<flight-plan xmlns="http://www8.garmin.com/xmlschemas/FlightPlan/v1">
<flight-data><etd-zulu>20260612T03:20:00Z</etd-zulu><altitude-ft>40000</altitude-ft></flight-data>
<waypoint-table>
  <waypoint><identifier>KEWR</identifier><type>AIRPORT</type><lat>40.6925</lat><lon>-74.1687</lon></waypoint>
  <waypoint><identifier>KAAO</identifier><type>AIRPORT</type><lat>37.7461</lat><lon>-97.2211</lon></waypoint>
</waypoint-table>
<route>
  <flight-plan-index>1</flight-plan-index>
  <route-point><waypoint-identifier>KEWR</waypoint-identifier><waypoint-type>AIRPORT</waypoint-type></route-point>
  <route-point><waypoint-identifier>KAAO</waypoint-identifier><waypoint-type>AIRPORT</waypoint-type></route-point>
</route>
</flight-plan>"#;

fn profile() -> ProfileParams {
    serde_json::from_str(
        r#"{ "cruise_tas_kt": 450, "climb_rate_fpm": 2500, "climb_tas_kt": 280,
             "descent_rate_fpm": 1800, "descent_tas_kt": 300, "cruise_burn_per_hour": 200,
             "taxi_fuel": 30, "fuel_capacity": 2000, "reserve_minutes": 45 }"#,
    )
    .expect("profile parses")
}

#[test]
fn an_invalid_fpl_is_an_invalid_param_error() {
    let plan = FlightPlan::from_fpl_bytes(b"<flight-plan></flight-plan>");
    assert!(plan.is_err(), "an empty plan must not parse to a route");
}

#[test]
fn cross_section_is_built_from_the_plan_and_profile() {
    let plan = FlightPlan::from_fpl_bytes(SAMPLE_FPL.as_bytes()).expect("plan parses");
    let (samples, ete, fuel, within) =
        build_cross_section(&plan, profile()).expect("cross-section");
    assert_eq!(samples.len(), 2);
    assert_eq!(samples[0].ident.as_deref(), Some("KEWR"));
    assert!(samples[0].altitude_ft < 1000.0, "departs low");
    assert!(ete.expect("ete") > 0);
    assert!(fuel.expect("fuel") > 0.0);
    assert_eq!(within, Some(true));
}

#[test]
fn product_names_parse_case_insensitively() {
    let kinds = parse_products(&["METAR".to_owned(), "taf".to_owned()]).expect("parse");
    assert_eq!(
        kinds,
        vec![
            aerocontext::ProductKind::Metar,
            aerocontext::ProductKind::Taf
        ]
    );
    assert!(parse_products(&["bogus".to_owned()]).is_err());
}
