#!/usr/bin/python3
"""Tests for slpkg/check_broken.py using pytest."""

import json
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest
from slpkg.check_broken import CheckBroken


@pytest.fixture
def mock_config():
    """Mock config_load for CheckBroken."""
    with patch("slpkg.check_broken.config_load") as mock:
        mock.deps_log_file = Path("/tmp/deps.log")
        mock.bold = ""
        mock.grey = ""
        mock.cyan = ""
        mock.red = ""
        mock.endc = ""
        yield mock


@pytest.fixture
def check_broken_inst(mock_config):
    """Fixture to create a CheckBroken instance."""
    with patch("slpkg.check_broken.Utilities"):
        inst = CheckBroken({"option_quiet": False})
        return inst


def test_read_deps_log_not_exists(check_broken_inst):
    """Test reading deps.log when it doesn't exist."""
    with patch("pathlib.Path.is_file", return_value=False):
        assert check_broken_inst._read_deps_log() == {}


def test_read_deps_log_valid(check_broken_inst):
    """Test reading valid deps.log."""
    data = {"pkg1": ["dep1", "dep2"]}
    with patch("pathlib.Path.is_file", return_value=True), \
         patch("pathlib.Path.read_text", return_value=json.dumps(data)):
        assert check_broken_inst._read_deps_log() == data


def test_read_deps_log_invalid_json(check_broken_inst):
    """Test reading invalid JSON deps.log."""
    with patch("pathlib.Path.is_file", return_value=True), \
         patch("pathlib.Path.read_text", return_value="not json"):
        assert check_broken_inst._read_deps_log() == {}


def test_run_no_broken(check_broken_inst, capsys):
    """Test run when no dependencies are broken."""
    deps_data = {"pkg1": ["dep1"]}
    check_broken_inst._read_deps_log = MagicMock(return_value=deps_data)
    # Both pkg and dep are installed
    check_broken_inst.utils.is_package_installed.return_value = "pkg1-1.0-x86_64-1"
    
    check_broken_inst.run()
    captured = capsys.readouterr()
    assert "No broken dependencies found." in captured.out


def test_run_with_broken(check_broken_inst, capsys):
    """Test run when some dependencies are missing."""
    deps_data = {"pkg1": ["dep1", "missing_dep"]}
    check_broken_inst._read_deps_log = MagicMock(return_value=deps_data)
    
    # Side effect for is_package_installed:
    # dep1 is installed, missing_dep is not
    def mock_is_installed(name):
        if name == "pkg1": return "pkg1-1.0"
        if name == "dep1": return "dep1-1.0"
        return False
        
    check_broken_inst.utils.is_package_installed.side_effect = mock_is_installed
    
    check_broken_inst.run()
    captured = capsys.readouterr()
    assert "Packages with missing dependencies:" in captured.out
    assert "pkg1" in captured.out
    assert "missing_dep" in captured.out
    assert "1 package with broken dependencies." in captured.out


def test_run_quiet_mode(check_broken_inst, capsys):
    """Test run in quiet mode."""
    check_broken_inst.option_quiet = True
    deps_data = {"pkg1": ["missing_dep"]}
    check_broken_inst._read_deps_log = MagicMock(return_value=deps_data)
    check_broken_inst.utils.is_package_installed.return_value = False
    
    check_broken_inst.run()
    captured = capsys.readouterr()
    # In quiet mode, only the package name should be printed
    assert captured.out.strip() == "pkg1"
