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

use aerocontext_core::{FlightRules, GeoPoint};
use chrono::TimeZone;

use super::*;

fn find<'a>(params: &'a [(&'static str, String)], key: &str) -> Option<&'a str> {
    params
        .iter()
        .find(|(k, _)| *k == key)
        .map(|(_, v)| v.as_str())
}

#[test]
fn area_form_carries_the_simple_non_briefing_markers() {
    let form = area_briefing_form(&AreaBriefingForm {
        location_identifier: "KSFO",
        departure: chrono::Utc.with_ymd_and_hms(2026, 6, 5, 14, 30, 0).unwrap(),
        radius_nm: 25,
    });
    assert_eq!(find(&form, "briefingType"), Some("SIMPLE"));
    assert_eq!(find(&form, "notABriefing"), Some("true"));
    assert_eq!(find(&form, "fixName"), Some("KSFO"));
    assert_eq!(find(&form, "briefingRadius"), Some("25"));
    assert_eq!(
        find(&form, "departureInstant"),
        Some("2026-06-05T14:30:00Z")
    );
}

#[test]
fn route_form_is_domestic_with_rules_endpoints_corridor_and_levels() {
    let form = route_briefing_form(&RouteBriefingForm {
        flight_rules: FlightRules::Ifr,
        departure: "KEWR",
        destination: "KAAO",
        departure_at: chrono::Utc.with_ymd_and_hms(2026, 6, 12, 3, 20, 0).unwrap(),
        route: "PSB",
        corridor_width_nm: 50,
        altitude_fl: Some(400),
        speed_kt: Some(450),
    });
    assert_eq!(find(&form, "type"), Some("DOMESTIC"));
    assert_eq!(find(&form, "flightRules"), Some("IFR"));
    assert_eq!(find(&form, "departure"), Some("KEWR"));
    assert_eq!(find(&form, "destination"), Some("KAAO"));
    assert_eq!(find(&form, "route"), Some("PSB"));
    assert_eq!(find(&form, "routeCorridorWidth"), Some("50"));
    assert_eq!(find(&form, "altitudeFL"), Some("400"));
    assert_eq!(find(&form, "speedKnots"), Some("450"));
    assert_eq!(find(&form, "briefingType"), Some("SIMPLE"));
}

#[test]
fn ddmm_location_rounds_to_whole_minutes_with_hemisphere() {
    let s = ddmm_location(GeoPoint {
        lat: 40.6925,
        lon: -74.1687,
    });
    assert_eq!(&s[4..5], "N");
    assert_eq!(&s[10..11], "W");
    assert_eq!(s.len(), 11);
}

#[test]
fn coordinate_route_fixes_use_the_slashless_form() {
    // A bare lat/lon (no slash) as an enroute fix, matching the area form.
    let coord = ddmm_location(GeoPoint {
        lat: 40.6925,
        lon: -74.1687,
    });
    let form = route_briefing_form(&RouteBriefingForm {
        flight_rules: FlightRules::Ifr,
        departure: "KEWR",
        destination: "KAAO",
        departure_at: chrono::Utc.with_ymd_and_hms(2026, 6, 12, 3, 20, 0).unwrap(),
        route: &coord,
        corridor_width_nm: 50,
        altitude_fl: None,
        speed_kt: None,
    });
    assert_eq!(find(&form, "route"), Some(coord.as_str()));
    assert!(!coord.contains('/'), "coordinate is slashless: {coord}");
}
