import pytest
from src.context_presets import ContextPresetManager
from src.models import ContextPreset, ContextFileEntry
from src.app_controller import AppController
from pathlib import Path
import tomli_w
import os
from unittest.mock import MagicMock

@pytest.fixture
def project_dict():
 return {
  "context_presets": {
   "test_preset": {
    "files": [{"path": "file1.py"}, {"path": "file2.py"}],
    "screenshots": ["shot1.png"],
    "description": "Test description"
   }
  }
 }

def test_manager_load_all(project_dict):
 manager = ContextPresetManager()
 presets = manager.load_all(project_dict)
 assert "test_preset" in presets
 assert len(presets["test_preset"].files) == 2
 assert presets["test_preset"].files[0].path == "file1.py"
 assert presets["test_preset"].screenshots == ["shot1.png"]
 assert presets["test_preset"].description == "Test description"

def test_manager_save_preset(project_dict):
 manager = ContextPresetManager()
 new_preset = ContextPreset(
  name="new_preset",
  files=[ContextFileEntry(path="new.py")],
  screenshots=["new.png"],
  description="New desc"
 )
 manager.save_preset(project_dict, new_preset)
 assert "new_preset" in project_dict["context_presets"]
 assert project_dict["context_presets"]["new_preset"]["description"] == "New desc"
 # ContextFileEntry.to_dict() includes view_mode and custom_slices
 assert project_dict["context_presets"]["new_preset"]["files"][0]["path"] == "new.py"
 assert project_dict["context_presets"]["new_preset"]["files"][0]["view_mode"] == "summary"

def test_manager_delete_preset(project_dict):
 manager = ContextPresetManager()
 manager.delete_preset(project_dict, "test_preset")
 assert "test_preset" not in project_dict["context_presets"]

def test_app_controller_save_load(tmp_path, monkeypatch):
 # Setup a dummy project
 project_file = tmp_path / "test_project.toml"
 project_data = {
  "project": {"name": "test"},
  "discussion": {"active": "main", "discussions": {"main": {"history": []}}},
  "files": {"paths": []},
  "screenshots": {"paths": []}
 }
 with open(project_file, "wb") as f:
  tomli_w.dump(project_data, f)
 
 # Mock directories to avoid issues during AppController init
 monkeypatch.setenv("SLOP_LOGS_DIR", str(tmp_path / "logs"))
 monkeypatch.setenv("SLOP_SCRIPTS_DIR", str(tmp_path / "scripts"))
 
 # Mocking some parts of AppController that might fail without full environment
 monkeypatch.setattr("src.paths.get_config_path", lambda: tmp_path / "config.toml")
 
 # Create dummy config
 with open(tmp_path / "config.toml", "wb") as f:
  tomli_w.dump({"ui": {"theme": "dark"}, "projects": []}, f)
 
 controller = AppController()
 controller.active_project_path = str(project_file)
 # We don't call init_state() as it does too much, we manually setup what we need
 controller.project = project_data
 controller._save_active_project = MagicMock()
 
 # Save preset
 preset = ContextPreset(
  name="saved_preset",
  files=[ContextFileEntry(path="app.py")],
  screenshots=["app.png"]
 )
 controller.save_context_preset(preset)
 
 # Verify in project dict
 assert "saved_preset" in controller.project["context_presets"]
 controller._save_active_project.assert_called_once()
 
 # Change state
 controller.context_files = []
 controller.screenshots = []
 
 # Load preset
 loaded = controller.load_context_preset("saved_preset")
 assert loaded.name == "saved_preset"
 assert [f.path for f in controller.context_files] == ["app.py"]
 assert controller.screenshots == ["app.png"]
 controller._save_active_project.assert_called()
