use super::common::{create_config, fixture_path};
use super::framework_convention_coverage_common::{
    collect_unused_exports, collect_unused_files, has_unused_export,
};

#[test]
fn docusaurus_custom_content_roots_and_swizzles_are_framework_used() {
    let root = fixture_path("docusaurus-advanced-conventions");
    let config = create_config(root.clone());
    let results = fallow_core::analyze(&config).expect("analysis should succeed");

    let unused_files = collect_unused_files(&root, &results);
    for expected_used_file in [
        "knowledge/getting-started.mdx",
        "community/welcome.mdx",
        "updates/hello-world.mdx",
        "site-pages/index.tsx",
        "public/scripts/runtime.js",
        "static-assets/styles/local.css",
        "src/theme/Root.tsx",
        "src/theme/Footer/index.tsx",
        "src/client/custom-script.js",
        "src/client/global-client.js",
        "src/css/custom.css",
        "src/css/extra.css",
        "src/css/global-client.css",
        "knowledge-sidebars.ts",
        "community-sidebars.ts",
        "versioned_docs/version-1.0.0/legacy.mdx",
        "community_versioned_docs/version-1.0.0/intro.mdx",
        "translations/fr/docusaurus-plugin-content-docs/current/translated.mdx",
        "translations/fr/docusaurus-plugin-content-docs-community/current/translated.mdx",
        "translations/fr/docusaurus-plugin-content-blog/translated-post.mdx",
        "translations/fr/docusaurus-plugin-content-pages/page.mdx",
    ] {
        assert!(
            !unused_files.iter().any(|path| path == expected_used_file),
            "{expected_used_file} should be treated as framework-used, unused files: {unused_files:?}"
        );
    }

    assert!(
        unused_files
            .iter()
            .any(|path| path == "src/helpers/orphan.ts"),
        "plain helper outside Docusaurus conventions should stay unused, unused files: {unused_files:?}"
    );

    let unused_exports = collect_unused_exports(&root, &results);
    for (path, export) in [
        ("site-pages/index.tsx", "default"),
        ("src/theme/Root.tsx", "default"),
        ("src/theme/Footer/index.tsx", "default"),
    ] {
        assert!(
            !has_unused_export(&unused_exports, path, export),
            "{path}:{export} should be framework-used, found: {unused_exports:?}"
        );
    }

    for (path, export) in [
        ("site-pages/index.tsx", "unusedPageHelper"),
        ("src/theme/Root.tsx", "unusedRootHelper"),
        ("src/theme/Footer/index.tsx", "unusedFooterHelper"),
    ] {
        assert!(
            has_unused_export(&unused_exports, path, export),
            "{path}:{export} should still be reported as unused, found: {unused_exports:?}"
        );
    }
}

#[test]
fn docusaurus_theme_only_sites_do_not_keep_content_conventions_alive() {
    let root = fixture_path("docusaurus-theme-only-conventions");
    let config = create_config(root.clone());
    let results = fallow_core::analyze(&config).expect("analysis should succeed");

    let unused_files = collect_unused_files(&root, &results);
    for expected_unused_file in [
        "docs/unused-doc.mdx",
        "src/pages/index.tsx",
        "sidebars.ts",
        "src/helpers/orphan.ts",
    ] {
        assert!(
            unused_files.iter().any(|path| path == expected_unused_file),
            "{expected_unused_file} should stay unused, unused files: {unused_files:?}"
        );
    }

    for expected_used_file in ["src/theme/Root.tsx", "src/theme/Navbar/index.tsx"] {
        assert!(
            !unused_files.iter().any(|path| path == expected_used_file),
            "{expected_used_file} should be treated as framework-used, unused files: {unused_files:?}"
        );
    }

    let unused_exports = collect_unused_exports(&root, &results);
    assert!(
        !has_unused_export(&unused_exports, "src/theme/Root.tsx", "default"),
        "theme swizzle default export should be framework-used, found: {unused_exports:?}"
    );
}

#[test]
fn docusaurus_autogenerated_sidebar_does_not_keep_sidebars_file_alive() {
    let root = fixture_path("docusaurus-classic-autogenerated-sidebar");
    let config = create_config(root.clone());
    let results = fallow_core::analyze(&config).expect("analysis should succeed");

    let unused_files = collect_unused_files(&root, &results);
    for expected_used_file in [
        "docs/intro.mdx",
        "src/pages/index.tsx",
        "src/theme/Root.tsx",
    ] {
        assert!(
            !unused_files.iter().any(|path| path == expected_used_file),
            "{expected_used_file} should be treated as framework-used, unused files: {unused_files:?}"
        );
    }

    assert!(
        unused_files.iter().any(|path| path == "sidebars.ts"),
        "sidebars.ts should stay unused when classic docs rely on autogenerated sidebars, unused files: {unused_files:?}"
    );
}
