use ryl::config::{Overrides, YamlLintConfig, discover_config};
use ryl::rules::anchors;

#[test]
fn anchors_allows_boolean_options() {
    let cfg = r#"
rules:
  anchors:
    forbid-undeclared-aliases: false
    forbid-duplicated-anchors: true
    forbid-unused-anchors: true
"#;

    let ctx = discover_config(
        &[],
        &Overrides {
            config_file: None,
            config_data: Some(cfg.into()),
        },
    )
    .expect("parse");

    assert!(ctx.config.rule_names().iter().any(|r| r == "anchors"));
}

#[test]
fn anchors_rejects_non_bool_option() {
    let cfg = r#"
rules:
  anchors:
    forbid-duplicated-anchors: "yes"
"#;

    let err = discover_config(
        &[],
        &Overrides {
            config_file: None,
            config_data: Some(cfg.into()),
        },
    )
    .expect_err("invalid value");

    assert!(err.contains("failed to parse config data:"), "{err}");
    assert!(err.contains("rules.anchors"), "{err}");
}

#[test]
fn anchors_rejects_unknown_option() {
    let cfg = r#"
rules:
  anchors:
    unknown: true
"#;

    let err = discover_config(
        &[],
        &Overrides {
            config_file: None,
            config_data: Some(cfg.into()),
        },
    )
    .expect_err("unknown option");

    assert!(err.contains("failed to parse config data:"), "{err}");
    assert!(err.contains("rules.anchors"), "{err}");
}

#[test]
fn anchors_toml_enables_ambiguous_names() {
    let cfg = YamlLintConfig::from_toml_str(
        "[rules.anchors]\nforbid-ambiguous-anchor-alias-names = true\n",
    )
    .expect("toml parse");
    let resolved = anchors::Config::resolve(&cfg);
    let hits = anchors::check("a: &foo: 1\n", &resolved);
    assert_eq!(hits.len(), 1, "expected one ambiguous diagnostic: {hits:?}");
    assert!(
        hits[0].message.contains("ambiguous anchor name \"foo:\""),
        "unexpected message: {hits:?}"
    );
}

#[test]
fn anchors_yaml_config_rejects_ambiguous_names_option() {
    let cfg = r#"
rules:
  anchors:
    forbid-ambiguous-anchor-alias-names: true
"#;

    let err = discover_config(
        &[],
        &Overrides {
            config_file: None,
            config_data: Some(cfg.into()),
        },
    )
    .expect_err("ryl-only option");

    assert!(err.contains("failed to parse config data:"), "{err}");
    assert!(err.contains("rules.anchors"), "{err}");
}

#[test]
fn anchors_rejects_non_string_option_key() {
    let cfg = r#"
rules:
  anchors:
    ? [1, 2]
    : true
"#;

    let err = discover_config(
        &[],
        &Overrides {
            config_file: None,
            config_data: Some(cfg.into()),
        },
    )
    .expect_err("non-string key");

    assert!(err.contains("cannot convert non-string TOML key"), "{err}");
}
