
import pytest
from unittest.mock import MagicMock, patch
import os
from pathlib import Path

# Mock imgui and other heavy dependencies before importing App
import sys
sys.modules['imgui_bundle'] = MagicMock()
sys.modules['imgui_bundle.imgui'] = MagicMock()
sys.modules['imgui_bundle.hello_imgui'] = MagicMock()
sys.modules['imgui_bundle.immapp'] = MagicMock()

# Mock tkinter
sys.modules['tkinter'] = MagicMock()
sys.modules['tkinter.filedialog'] = MagicMock()

# Mock ai_client and session_logger
sys.modules['ai_client'] = MagicMock()
sys.modules['session_logger'] = MagicMock()

from gui_2 import App

@pytest.fixture
def mock_config(tmp_path):
    config_path = tmp_path / "config.toml"
    config_path.write_text("[projects]
paths = []
active = ""
[ai]
provider = "gemini"
model = "model"
", encoding="utf-8")
    return config_path

@pytest.fixture
def mock_project(tmp_path):
    project_path = tmp_path / "project.toml"
    project_path.write_text("[project]
name = "test"
[discussion]
roles = ["User", "AI"]
active = "main"
[discussion.discussions.main]
history = []
", encoding="utf-8")
    return project_path

def test_log_management_init(mock_config, mock_project, monkeypatch):
    monkeypatch.setattr("gui_2.CONFIG_PATH", mock_config)
    
    with patch("project_manager.load_project") as mock_load:
        mock_load.return_value = {
            "project": {"name": "test"},
            "discussion": {"roles": ["User", "AI"], "active": "main", "discussions": {"main": {"history": []}}},
            "files": {"paths": []},
            "screenshots": {"paths": []}
        }
        with patch("session_logger.open_session"):
            app = App()
            
            # Check if Log Management is in show_windows
            assert "Log Management" in app.show_windows
            assert app.show_windows["Log Management"] is False # Default as set in __init__
            
            # Check if _render_log_management exists
            assert hasattr(app, "_render_log_management")
            assert callable(app._render_log_management)

def test_render_log_management_logic(mock_config, mock_project, monkeypatch):
    monkeypatch.setattr("gui_2.CONFIG_PATH", mock_config)
    
    with patch("project_manager.load_project") as mock_load:
        mock_load.return_value = {
            "project": {"name": "test"},
            "discussion": {"roles": ["User", "AI"], "active": "main", "discussions": {"main": {"history": []}}},
            "files": {"paths": []},
            "screenshots": {"paths": []}
        }
        with patch("session_logger.open_session"):
            app = App()
            app.show_windows["Log Management"] = True
            
            from imgui_bundle import imgui
            
            # Mock LogRegistry
            with patch("gui_2.LogRegistry") as MockRegistry:
                mock_reg = MockRegistry.return_value
                mock_reg.data = {
                    "session_1": {
                        "start_time": "2023-01-01",
                        "whitelisted": False,
                        "metadata": {"reason": "test", "size_kb": 10, "message_count": 5}
                    }
                }
                
                # Mock imgui.begin to return (True, True)
                imgui.begin.return_value = (True, True)
                imgui.begin_table.return_value = True
                
                # Call render
                app._render_log_management()
                
                # Verify imgui calls
                imgui.begin.assert_called_with("Log Management", True)
                imgui.begin_table.assert_called()
                
                # Check for "session_1" text
                imgui.text.assert_any_call("session_1")
