import pytest
from pathlib import Path
from src.workspace_manager import WorkspaceManager
from src.models import WorkspaceProfile
from src import paths

def test_load_all_profiles_merged(tmp_path, monkeypatch):
 global_profiles_path = tmp_path / "global_workspace_profiles.toml"
 project_root = tmp_path / "project"
 project_root.mkdir()
 project_profiles_path = project_root / ".ai" / "workspace_profiles.toml"
 project_profiles_path.parent.mkdir()

 monkeypatch.setattr(paths, "get_global_workspace_profiles_path", lambda: global_profiles_path)
 monkeypatch.setattr(paths, "get_project_workspace_profiles_path", lambda p: project_profiles_path)

 global_content = """
[profiles.GlobalOnly]
ini_content = "global_ini"
show_windows = { "win1" = true }
panel_states = { "p1" = 1 }

[profiles.Overridden]
ini_content = "global_ini_to_be_overridden"
show_windows = { "win2" = false }
panel_states = { "p2" = 2 }
"""
 global_profiles_path.write_text(global_content)

 project_content = """
[profiles.ProjectOnly]
ini_content = "project_ini"
show_windows = { "win3" = true }
panel_states = { "p3" = 3 }

[profiles.Overridden]
ini_content = "project_ini_override"
show_windows = { "win2" = true }
panel_states = { "p2" = 22 }
"""
 project_profiles_path.write_text(project_content)

 wm = WorkspaceManager(project_root=project_root)
 profiles = wm.load_all_profiles()

 assert len(profiles) == 3
 assert "GlobalOnly" in profiles
 assert "ProjectOnly" in profiles
 assert "Overridden" in profiles

 assert profiles["GlobalOnly"].ini_content == "global_ini"
 assert profiles["ProjectOnly"].ini_content == "project_ini"
 assert profiles["Overridden"].ini_content == "project_ini_override"
 assert profiles["Overridden"].show_windows["win2"] is True
 assert profiles["Overridden"].panel_states["p2"] == 22

def test_save_profile_global_and_project(tmp_path, monkeypatch):
 global_profiles_path = tmp_path / "global_workspace_profiles.toml"
 project_root = tmp_path / "project"
 project_root.mkdir()
 project_profiles_path = project_root / ".ai" / "workspace_profiles.toml"
 project_profiles_path.parent.mkdir()

 monkeypatch.setattr(paths, "get_global_workspace_profiles_path", lambda: global_profiles_path)
 monkeypatch.setattr(paths, "get_project_workspace_profiles_path", lambda p: project_profiles_path)

 wm = WorkspaceManager(project_root=project_root)

 profile_global = WorkspaceProfile(name="MyGlobal", ini_content="g_ini", show_windows={}, panel_states={})
 wm.save_profile(profile_global, scope="global")

 assert global_profiles_path.exists()
 loaded_global = wm.load_all_profiles()
 assert "MyGlobal" in loaded_global
 assert loaded_global["MyGlobal"].ini_content == "g_ini"

 profile_project = WorkspaceProfile(name="MyProject", ini_content="p_ini", show_windows={}, panel_states={})
 wm.save_profile(profile_project, scope="project")

 assert project_profiles_path.exists()
 loaded_all = wm.load_all_profiles()
 assert "MyGlobal" in loaded_all
 assert "MyProject" in loaded_all
 assert loaded_all["MyProject"].ini_content == "p_ini"

def test_delete_profile(tmp_path, monkeypatch):
 global_profiles_path = tmp_path / "global_workspace_profiles.toml"
 project_root = tmp_path / "project"
 project_root.mkdir()
 project_profiles_path = project_root / ".ai" / "workspace_profiles.toml"
 project_profiles_path.parent.mkdir()

 monkeypatch.setattr(paths, "get_global_workspace_profiles_path", lambda: global_profiles_path)
 monkeypatch.setattr(paths, "get_project_workspace_profiles_path", lambda p: project_profiles_path)

 wm = WorkspaceManager(project_root=project_root)

 p1 = WorkspaceProfile(name="P1", ini_content="1", show_windows={}, panel_states={})
 p2 = WorkspaceProfile(name="P2", ini_content="2", show_windows={}, panel_states={})

 wm.save_profile(p1, scope="global")
 wm.save_profile(p2, scope="project")

 profiles = wm.load_all_profiles()
 assert "P1" in profiles
 assert "P2" in profiles

 wm.delete_profile("P2", scope="project")
 profiles = wm.load_all_profiles()
 assert "P1" in profiles
 assert "P2" not in profiles

 wm.delete_profile("P1", scope="global")
 profiles = wm.load_all_profiles()
 assert "P1" not in profiles
