#![allow(
    clippy::unwrap_in_result,
    reason = "unwraps are allowed anywhere in tests"
)]
#![allow(
    clippy::indexing_slicing,
    reason = "unwraps are allowed anywhere in tests"
)]

use std::assert_matches;

use super::{Code, Warning};
use crate::{
    json::{self, FromJson as _},
    warning::test::VerdictTestExt as _,
    Verdict,
};

impl Code {
    /// Return a `currency::Code` for the given `str`.
    ///
    /// # Panics
    ///
    /// Will panic when the given `str` is not three bytes long or an unknown currency code.
    pub fn from_alpha_3_str(code: &str) -> Self {
        let bytes = code.as_bytes();

        // ISO 4217 is expected to be 3 chars.
        let [a, b, c] = bytes else {
            panic!(
                "Unable to parse currency code. Expected a length of 3 chars. It has length: `{}`",
                code.len()
            );
        };

        let triplet: [u8; 3] = [
            a.to_ascii_uppercase(),
            b.to_ascii_uppercase(),
            c.to_ascii_uppercase(),
        ];

        let Some(code) = Code::from_alpha_3(triplet) else {
            panic!("Unknown currency code `{code}`");
        };

        code
    }
}

#[test]
fn should_create_currency_without_issue() {
    const JSON: &str = r#"{ "currency": "EUR" }"#;

    let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();

    assert_eq!(Code::Eur, code);
    assert!(warnings.is_empty(), "{:#?}", warnings.path_id_map());
}

#[test]
fn should_raise_currency_content_issue() {
    const JSON: &str = r#"{ "currency": "VVV" }"#;

    let error = parse_code_from_json(JSON).unwrap_only_error();

    assert_matches!(error.into_warning(), Warning::InvalidCode);
}

#[test]
fn should_raise_currency_case_issue() {
    const JSON: &str = r#"{ "currency": "eur" }"#;

    let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
    let warnings = warnings.path_map();
    let warnings = &*warnings["$.currency"];

    assert_eq!(code, Code::Eur);
    assert_matches!(warnings, [Warning::PreferUpperCase]);
}

#[test]
fn should_raise_currency_xts_issue() {
    const JSON: &str = r#"{ "currency": "xts" }"#;

    let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
    let warnings = warnings.path_map();
    let warnings = &*warnings["$.currency"];

    assert_eq!(code, Code::Xts);
    assert_matches!(
        warnings,
        [Warning::PreferUpperCase, Warning::InvalidCodeXTS]
    );
}

#[test]
fn should_raise_currency_xxx_issue() {
    const JSON: &str = r#"{ "currency": "xxx" }"#;

    let (code, warnings) = parse_code_from_json(JSON).unwrap().into_parts();
    let warnings = warnings.path_map();
    let warnings = &*warnings["$.currency"];

    assert_eq!(code, Code::Xxx);
    assert_matches!(
        warnings,
        [Warning::PreferUpperCase, Warning::InvalidCodeXXX]
    );
}

#[track_caller]
fn parse_code_from_json(json: &str) -> Verdict<Code, Warning> {
    let doc = json::parse(json.into()).unwrap();
    let currency_elem = doc.root().find_field("currency").unwrap();
    Code::from_json(currency_elem.element())
}
