import os
import pytest
import copy
from src import models
from src.app_controller import AppController

@pytest.fixture
def controller(tmp_path):
 # Create a mock project file
 proj_path = tmp_path / "test_project.toml"
 proj_path.write_text("[project]\nname = 'test'\n")
 
 ctrl = AppController()
 ctrl.active_project_path = str(proj_path)
 ctrl.project = {"project": {"name": "test"}}
 ctrl.view_presets = []
 
 # Initialize missing attributes needed for flush/refresh
 ctrl.ui_output_dir = "./md_gen"
 ctrl.ui_files_base_dir = "."
 ctrl.ui_shots_base_dir = "."
 ctrl.ui_project_git_dir = ""
 ctrl.ui_project_conductor_dir = "conductor"
 ctrl.ui_project_system_prompt = ""
 ctrl.ui_project_preset_name = None
 ctrl.ui_gemini_cli_path = "gemini"
 ctrl.ui_word_wrap = True
 ctrl.ui_auto_add_history = False
 ctrl.ui_auto_scroll_comms = True
 ctrl.ui_auto_scroll_tool_calls = True
 ctrl.ui_agent_tools = {}
 ctrl.ui_epic_input = ""
 ctrl.ui_active_context_preset = ""
 ctrl.preset_manager = type('Mock', (), {'load_all': lambda self: {}, 'project_root': None})()
 ctrl.tool_preset_manager = type('Mock', (), {'load_all_presets': lambda self: {}, 'load_all_bias_profiles': lambda self: {}, 'project_root': None})()
 ctrl.persona_manager = type('Mock', (), {'load_all': lambda self: {}})()
 
 return ctrl

def test_save_view_preset(controller):
 f_item = models.FileItem(path="test.py", view_mode="skeleton")
 f_item.ast_mask = {"test::func": "sig"}
 f_item.custom_slices = [{"start_line": 1, "end_line": 10}]
 
 controller._cb_save_view_preset("my_preset", f_item)
 
 assert any(vp.name == "my_preset" for vp in controller.view_presets)
 preset = next(vp for vp in controller.view_presets if vp.name == "my_preset")
 assert preset.view_mode == "skeleton"
 assert preset.ast_mask == {"test::func": "sig"}
 assert preset.custom_slices == [{"start_line": 1, "end_line": 10}]
 
 # Verify persistence
 controller._flush_to_project()
 assert "view_presets" in controller.project
 assert isinstance(controller.project["view_presets"], list)
 assert any(vp["name"] == "my_preset" for vp in controller.project["view_presets"])

def test_apply_view_preset(controller):
 # Setup a preset
 preset = models.NamedViewPreset(
  name="my_preset",
  view_mode="masked",
  ast_mask={"main::run": "def"},
  custom_slices=[{"start_line": 5, "end_line": 15}]
 )
 controller.view_presets.append(preset)
 
 # Create a file item to apply to
 f_item = models.FileItem(path="main.py", view_mode="summary")
 
 controller._cb_apply_view_preset("my_preset", f_item)
 
 assert f_item.view_mode == "masked"
 assert f_item.ast_mask == {"main::run": "def"}
 assert f_item.custom_slices == [{"start_line": 5, "end_line": 15}]

def test_delete_view_preset(controller):
 preset = models.NamedViewPreset(name="to_del", view_mode="full")
 controller.view_presets.append(preset)
 
 controller._cb_delete_view_preset("to_del")
 
 assert not any(vp.name == "to_del" for vp in controller.view_presets)

def test_load_presets_from_project_list(controller):
 controller.project["view_presets"] = [
  {
   "name": "stored_preset",
   "view_mode": "outline",
   "ast_mask": {"a": "b"},
   "custom_slices": []
  }
 ]
 
 controller._refresh_from_project()
 
 assert any(vp.name == "stored_preset" for vp in controller.view_presets)
 preset = next(vp for vp in controller.view_presets if vp.name == "stored_preset")
 assert preset.view_mode == "outline"
 assert preset.ast_mask == {"a": "b"}

def test_load_presets_from_project_legacy_dict(controller):
 # Test backward compatibility
 controller.project["view_presets"] = {
  "legacy_preset": {
   "view_mode": "full",
   "ast_mask": {"c": "d"},
   "custom_slices": []
  }
 }
 
 controller._refresh_from_project()
 
 assert any(vp.name == "legacy_preset" for vp in controller.view_presets)
 preset = next(vp for vp in controller.view_presets if vp.name == "legacy_preset")
 assert preset.view_mode == "full"
 assert preset.ast_mask == {"c": "d"}
