mod common;

use std::path::PathBuf;

use ryl::config::{Overrides, discover_config_with};

fn config_from_toml(body: &str) -> ryl::config::YamlLintConfig {
    let path = PathBuf::from("/repo/.ryl.toml");
    let env = common::fake_env::FakeEnv::new().with_file(path.clone(), body);
    discover_config_with(
        &[],
        &Overrides {
            config_file: Some(path),
            config_data: None,
        },
        &env,
    )
    .expect("toml config should parse")
    .config
}

#[test]
fn markdown_sources_default_to_enabled() {
    let cfg = config_from_toml("files = { markdown = [\"*.md\"] }\n");
    assert!(cfg.markdown_front_matter());
    assert!(cfg.markdown_fenced_blocks());
}

#[test]
fn markdown_source_toggles_are_applied() {
    let cfg = config_from_toml(
        "files = { markdown = [\"*.md\"] }\nmarkdown = { front-matter = false, fenced-blocks = true }\n",
    );
    assert!(!cfg.markdown_front_matter());
    assert!(cfg.markdown_fenced_blocks());
}

#[test]
fn markdown_candidate_matches_only_with_files_pattern() {
    let base = PathBuf::from("/repo");
    let doc = PathBuf::from("/repo/readme.md");

    let enabled = config_from_toml("files = { markdown = [\"*.md\"] }\n");
    assert!(enabled.is_markdown_candidate(&doc, &base));
    let outside_base = PathBuf::from("/elsewhere/notes.md");
    assert!(enabled.is_markdown_candidate(&outside_base, &base));

    let disabled = config_from_toml("[rules]\ncolons = \"enable\"\n");
    assert!(!disabled.is_markdown_candidate(&doc, &base));
}

#[test]
fn to_toml_string_round_trips_markdown_settings() {
    let cfg = config_from_toml(
        "files = { markdown = [\"*.md\"] }\nmarkdown = { front-matter = false }\n",
    );
    let rendered = cfg.to_toml_string();

    assert!(rendered.contains("[files]"), "{rendered}");
    assert!(rendered.contains("markdown = [\"*.md\"]"), "{rendered}");
    assert!(rendered.contains("[markdown]"), "{rendered}");
    assert!(rendered.contains("front-matter = false"), "{rendered}");
    assert!(rendered.contains("fenced-blocks = true"), "{rendered}");
}

#[test]
fn yaml_files_key_is_rejected_in_toml() {
    let path = PathBuf::from("/repo/.ryl.toml");
    let env = common::fake_env::FakeEnv::new()
        .with_file(path.clone(), "yaml-files = [\"*.yaml\"]\n");
    let err = discover_config_with(
        &[],
        &Overrides {
            config_file: Some(path),
            config_data: None,
        },
        &env,
    )
    .expect_err("yaml-files in TOML must be rejected");
    assert!(
        err.contains("yaml-files") && err.contains("[files]"),
        "{err}"
    );
}

#[test]
fn unknown_keys_in_files_and_markdown_tables_are_rejected() {
    let path = PathBuf::from("/repo/.ryl.toml");
    for body in [
        "[files]\nyml = [\"*.md\"]\n",
        "[markdown]\nfrontmatter = false\n",
    ] {
        let env = common::fake_env::FakeEnv::new().with_file(path.clone(), body);
        let err = discover_config_with(
            &[],
            &Overrides {
                config_file: Some(path.clone()),
                config_data: None,
            },
            &env,
        )
        .expect_err("typo'd key must be rejected");
        assert!(!err.is_empty(), "expected an error for: {body}");
    }
}
