import pytest
import os
import time
from pathlib import Path
import tomli_w
from src.api_hook_client import ApiHookClient

def test_preset_switching(live_gui, live_gui_workspace):
 client = ApiHookClient()
 client.click("btn_reset")
 time.sleep(2)
 
 # Paths for presets
 temp_workspace = live_gui_workspace
 global_presets_path = temp_workspace / "presets.toml"
 project_presets_path = temp_workspace / "project_presets.toml"
 manual_slop_path = temp_workspace / "manual_slop.toml"
 
 # Cleanup before test
 if global_presets_path.exists(): global_presets_path.unlink()
 if project_presets_path.exists(): project_presets_path.unlink()
 
 try:
  # Create a global preset
  global_presets_path.write_text(tomli_w.dumps({
   "presets": {
    "TestGlobal": {
     "system_prompt": "Global Prompt"
    }
   }
  }))
  
  # Create a project preset
  project_presets_path.write_text(tomli_w.dumps({
   "presets": {
    "TestProject": {
     "system_prompt": "Project Prompt"
    },
    "TestGlobal": { # Override
     "system_prompt": "Overridden Prompt"
    }
   }
  }))
  
  # Switch to the local project to ensure context is correct
  client.push_event("custom_callback", {
   "callback": "_switch_project",
   "args": [str(manual_slop_path.absolute())]
  })
  time.sleep(2)
  
  # Trigger reload of presets (just in case)
  client.push_event("custom_callback", {
   "callback": "_refresh_from_project",
   "args": []
  })
  time.sleep(2) # Wait for processing
  
  # Apply Global Preset (should use override from project if available in merged list)
  client.push_event("custom_callback", {
   "callback": "_apply_preset",
   "args": ["TestGlobal", "global"]
  })
  time.sleep(2)
  
  # Verify state
  state = client.get_gui_state()
  assert state.get("global_preset_name") == "TestGlobal", f"Expected TestGlobal, got {state.get('global_preset_name')}. Full state: {state}"
  assert state.get("global_system_prompt") == "Overridden Prompt", f"Expected Overridden Prompt, got {state.get('global_system_prompt')}"
  
  # Apply Project Preset
  client.push_event("custom_callback", {
   "callback": "_apply_preset",
   "args": ["TestProject", "project"]
  })
  time.sleep(2)
  
  state = client.get_gui_state()
  assert state.get("project_preset_name") == "TestProject", f"Expected TestProject, got {state.get('project_preset_name')}. Full state: {state}"
  assert state.get("project_system_prompt") == "Project Prompt", f"Expected Project Prompt, got {state.get('project_system_prompt')}"
  
  # Select "None"
  client.push_event("custom_callback", {
   "callback": "_apply_preset",
   "args": ["None", "global"]
  })
  time.sleep(2)
  state = client.get_gui_state()
  assert not state.get("global_preset_name"), f"Expected global_preset_name to be empty, got {state.get('global_preset_name')}" # Should be None or ""
  
  # Prompt remains from previous application
  assert state.get("global_system_prompt") == "Overridden Prompt"

 finally:
  # Cleanup
  if global_presets_path.exists(): global_presets_path.unlink()
  if project_presets_path.exists(): project_presets_path.unlink()

def test_preset_manager_modal(live_gui, live_gui_workspace):
 client = ApiHookClient()
 client.click("btn_reset")
 time.sleep(2)
 
 # Open Preset Manager
 client.push_event("custom_callback", {"callback": "_set_attr", "args": ["show_preset_manager_window", True]})
 time.sleep(1)
 state = client.get_gui_state()
 assert state.get("show_preset_manager_window") is True
 
 # Create a new preset via fields
 client.set_value("editing_preset_name", "TestNew")
 client.set_value("editing_preset_system_prompt", "New Prompt Text")
 # EXPLICITLY set scope to global
 client.set_value("_editing_preset_scope", "global")
 
 # Click Save (maps to 'save_preset' action)
 client.click("save_preset")
 time.sleep(1)
 
 # Verify it exists in file
 global_presets_path = live_gui_workspace / "presets.toml"
 # assert global_presets_path.exists(), f"Global presets file not found at {global_presets_path}. Full state: {client.get_gui_state()}"

