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

use ocpi_tariffs::{guess, lint, tariff, warning, Version};

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

    let report = tariff::parse_and_report(TARIFF_JSON).unwrap();
    let guess::Report {
        unexpected_fields,
        version,
    } = report;

    if !unexpected_fields.is_empty() {
        eprintln!("Strange... there are fields in the tariff that are not defined in the spec.");

        for path in &unexpected_fields {
            eprintln!("  * {path}");
        }

        eprintln!();
    }

    let tariff = version.certain_or(Version::V221);
    let report = tariff::lint(&tariff);

    print_lint_warnings(&report.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)
    );
}
