import pytest
from ImportNinja.analyzer import (
    get_requirements_imports,
    check_requirements_file_up_to_date,
    requirements_file_analysis,
    write_requirements_file
)


@pytest.fixture
def temp_requirements_file(tmp_path):
    req_file = tmp_path / "requirements.txt"
    req_file.write_text("requests\nnumpy\n")
    return req_file


def test_get_requirements_imports(temp_requirements_file):
    imports = get_requirements_imports(temp_requirements_file)
    assert imports == {"requests", "numpy"}


def test_get_requirements_imports_no_file():
    imports = get_requirements_imports("non_existent_file.txt")
    assert imports == set()


def test_check_requirements_file_up_to_date():
    requirements_imports = {"requests", "numpy"}
    all_imports = {"requests", "numpy"}
    assert check_requirements_file_up_to_date(requirements_imports, all_imports) is True


def test_check_requirements_file_not_up_to_date():
    requirements_imports = {"requests"}
    all_imports = {"requests", "numpy"}
    assert check_requirements_file_up_to_date(requirements_imports, all_imports) is False


def test_requirements_file_analysis_up_to_date(capfd):
    requirements_imports = {"requests", "numpy"}
    all_imports = {"requests", "numpy"}
    requirements_file_analysis(requirements_imports, all_imports, True)
    captured = capfd.readouterr()
    assert "Requirements file is up to date." in captured.out


def test_requirements_file_analysis_missing_imports(capfd):
    requirements_imports = {"requests"}
    all_imports = {"requests", "numpy"}
    requirements_file_analysis(requirements_imports, all_imports, False)
    captured = capfd.readouterr()
    assert "Imports missing in requirements.txt:" in captured.out
    assert "- numpy" in captured.out


def test_requirements_file_analysis_unused_imports(capfd):
    requirements_imports = {"requests", "numpy", "pandas"}
    all_imports = {"requests", "numpy"}
    requirements_file_analysis(requirements_imports, all_imports, False)
    captured = capfd.readouterr()
    assert "Imports in requirements.txt not used:" in captured.out
    assert "- pandas" in captured.out


def test_requirements_file_analysis_no_requirements_file(capfd):
    requirements_imports = set()
    all_imports = {"requests", "numpy"}
    requirements_file_analysis(requirements_imports, all_imports, False)
    captured = capfd.readouterr()
    assert "No requirements.txt file found." in captured.out


def test_write_requirements_file(tmp_path):
    requirements_file = tmp_path / "requirements.txt"
    all_imports = {"requests", "numpy", "os"}

    # Mocking sys.stdlib_module_names for this test
    import sys
    original_stdlib_modules = sys.stdlib_module_names
    sys.stdlib_module_names = {"os", "sys"}

    write_requirements_file(requirements_file, all_imports)
    contents = requirements_file.read_text().splitlines()

    assert "requests" in contents
    assert "numpy" in contents
    assert "os" not in contents  # os should not be included as it's a stdlib module

    # Restore the original stdlib modules after the test
    sys.stdlib_module_names = original_stdlib_modules
