#![expect(clippy::unwrap_used, reason = "examples can panic")]
#![expect(clippy::print_stderr, reason = "examples can log to stderr")]

use ocpi_tariffs::{json, lint, schema, tariff, warning, Version};

fn main() {
    const TARIFF_JSON: &str = include_str!("data/tariff_misspelled_field.json");

    // Parse the raw JSON and validate it against the `v2.2.1` tariff schema. Any unexpected,
    // misspelled, missing, or wrongly typed fields are reported as schema warnings.
    let doc = json::parse_object(TARIFF_JSON).unwrap();
    let (tariff, warnings) = tariff::build(doc, Version::V221).into_parts();

    print_schema_warnings(&warnings);

    let report = tariff::lint(&tariff);

    print_lint_warnings(&report.warnings);
}

/// Print `schema::Warning`s to `stderr`.
fn print_schema_warnings(warnings: &warning::Set<schema::Warning>) {
    if warnings.is_empty() {
        return;
    }

    eprintln!(
        "WARN: {} schema warnings from the tariff:\n {}",
        warnings.len_warnings(),
        warning::SetWriter::new(warnings)
    );
}

/// Print `lint::tariff::Warning`s to `stderr`.
fn print_lint_warnings(warnings: &warning::Set<lint::tariff::Warning>) {
    if warnings.is_empty() {
        return;
    }

    eprintln!(
        "WARN: {} warnings from the linting:\n {}",
        warnings.len_warnings(),
        warning::SetWriter::new(warnings)
    );
}
