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

@pytest.fixture(scope="session", autouse=True)
def test_env_setup():
 temp_workspace = Path("tests/artifacts/live_gui_workspace")
 if temp_workspace.exists():
  try: shutil.rmtree(temp_workspace)
  except: pass
 temp_workspace.mkdir(parents=True, exist_ok=True)
 
 config_path = temp_workspace / "config.toml"
 manual_slop_path = temp_workspace / "manual_slop.toml"
 
 # Create minimal project file
 manual_slop_path.write_text("[project]\nname = 'TestProject'\n", encoding="utf-8")
 
 # Create local config.toml
 config_path.write_text(tomli_w.dumps({
  "projects": {
   "paths": [str(manual_slop_path.absolute())],
   "active": str(manual_slop_path.absolute())
  },
  "ai": {
   "provider": "gemini",
   "model": "gemini-2.5-flash-lite"
  }
 }))
 yield
 # Cleanup handled by live_gui fixture usually, but we can be explicit
 if config_path.exists(): config_path.unlink()

def test_preset_switching(live_gui):
 client = ApiHookClient()
 
 # Paths for presets
 temp_workspace = Path("tests/artifacts/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):
 client = ApiHookClient()
 temp_workspace = Path("tests/artifacts/live_gui_workspace")
 global_presets_path = temp_workspace / "presets.toml"
 project_presets_path = temp_workspace / "project_presets.toml"
 
 # Open Modal
 client.set_value("show_preset_manager_modal", True)
 time.sleep(2)
 
 # Create New Preset via Modal Logic (triggering the callback directly for reliability in headless)
 client.push_event("custom_callback", {
  "callback": "_cb_save_preset",
  "args": ["ModalPreset", "Modal Content", "global"]
 })
 time.sleep(3)
 
 # Verify file exists
 if not global_presets_path.exists():
  state = client.get_gui_state()
  assert global_presets_path.exists(), f"Global presets file not found at {global_presets_path}. Full state: {state}"

 with open(global_presets_path, "rb") as f:
  import tomllib
  data = tomllib.load(f)
  assert "ModalPreset" in data["presets"]
  assert data["presets"]["ModalPreset"]["system_prompt"] == "Modal Content"

 
 # Delete Preset via Modal Logic
 client.push_event("custom_callback", {
  "callback": "_cb_delete_preset",
  "args": ["ModalPreset", "global"]
 })
 time.sleep(2)
 
 # Verify file content
 with open(global_presets_path, "rb") as f:
  data = tomllib.load(f)
  assert "ModalPreset" not in data["presets"]
