import pytest
import tomli_w
from pathlib import Path
from src.tool_presets import ToolPresetManager
from src.models import ToolPreset, BiasProfile, Tool
from src import paths

@pytest.fixture
def temp_paths(tmp_path, monkeypatch):
 global_dir = tmp_path / "global"
 global_dir.mkdir()
 project_dir = tmp_path / "project"
 project_dir.mkdir()
 
 global_presets = global_dir / "tool_presets.toml"
 project_presets = project_dir / "project_tool_presets.toml"
 
 monkeypatch.setattr(paths, "get_global_tool_presets_path", lambda: global_presets)
 monkeypatch.setattr(paths, "get_project_tool_presets_path", lambda _: project_presets)
 
 return {
  "global_dir": global_dir,
  "project_dir": project_dir,
  "global_presets": global_presets,
  "project_presets": project_presets
 }

def test_load_all_presets_merged(temp_paths):
 # Setup global presets
 global_data = {
  "presets": {
   "default": {
    "categories": {
     "General": [{"name": "read_file", "approval": "auto"}]
    }
   },
   "global_only": {
    "categories": {"Web": [{"name": "web_search", "approval": "ask"}]}
   }
  }
 }
 with open(temp_paths["global_presets"], "wb") as f:
  tomli_w.dump(global_data, f)
  
 # Setup project presets (overrides 'default')
 project_data = {
  "presets": {
   "default": {
    "categories": {
     "General": [{"name": "read_file", "approval": "auto", "weight": 5}]
    }
   }
  }
 }
 with open(temp_paths["project_presets"], "wb") as f:
  tomli_w.dump(project_data, f)
  
 manager = ToolPresetManager(project_root=temp_paths["project_dir"])
 all_presets = manager.load_all_presets()
 
 assert "default" in all_presets
 assert isinstance(all_presets["default"].categories["General"][0], Tool)
 assert all_presets["default"].categories["General"][0].weight == 5
 assert "global_only" in all_presets

def test_bias_profiles_merged(temp_paths):
 # Setup global biases
 global_data = {
  "bias_profiles": {
   "Discovery": {
    "tool_weights": {"web_search": 5},
    "category_multipliers": {"Web": 1.5}
   }
  }
 }
 with open(temp_paths["global_presets"], "wb") as f:
  tomli_w.dump(global_data, f)
  
 # Setup project biases
 project_data = {
  "bias_profiles": {
   "Execution": {
    "tool_weights": {"run_powershell": 5}
   }
  }
 }
 with open(temp_paths["project_presets"], "wb") as f:
  tomli_w.dump(project_data, f)
  
 manager = ToolPresetManager(project_root=temp_paths["project_dir"])
 profiles = manager.load_all_bias_profiles()
 
 assert "Discovery" in profiles
 assert profiles["Discovery"].category_multipliers["Web"] == 1.5
 assert "Execution" in profiles
 assert profiles["Execution"].tool_weights["run_powershell"] == 5

def test_save_bias_profile(temp_paths):
 manager = ToolPresetManager(project_root=temp_paths["project_dir"])
 profile = BiasProfile(name="Custom", tool_weights={"test": 1})
 
 manager.save_bias_profile(profile, scope="project")
 
 loaded = manager.load_all_bias_profiles()
 assert "Custom" in loaded
 assert loaded["Custom"].tool_weights["test"] == 1

def test_delete_bias_profile(temp_paths):
 project_data = {
  "bias_profiles": {
   "to_delete": {"tool_weights": {}}
  }
 }
 with open(temp_paths["project_presets"], "wb") as f:
  tomli_w.dump(project_data, f)
  
 manager = ToolPresetManager(project_root=temp_paths["project_dir"])
 manager.delete_bias_profile("to_delete", scope="project")
 
 profiles = manager.load_all_bias_profiles()
 assert "to_delete" not in profiles
