import pytest
from pathlib import Path
from src import app_controller, beads_client, models
import tomli_w

def test_load_active_tickets_from_beads(tmp_path: Path):
    # 1. Setup mock Beads repository
    workspace_dir = tmp_path / "workspace"
    workspace_dir.mkdir()
    bclient = beads_client.BeadsClient(workspace_dir)
    bclient.init_repo()
    bclient.create_bead(title="Bead 1", description="Description 1")
    
    # 2. Setup mock project file
    proj_path = workspace_dir / "project.toml"
    proj_data = {
        "project": {
            "name": "test_project",
            "execution_mode": "beads"
        },
        "mma": {
            "active_track": {
                "id": "track_20260309",
                "description": "Mock Track",
                "tickets": []
            }
        }
    }
    with open(proj_path, "wb") as f:
        tomli_w.dump(proj_data, f)
        
    # 3. Initialize AppController (minimal)
    ctrl = app_controller.AppController()
    ctrl.active_project_path = str(proj_path)
    ctrl.project = proj_data
    ctrl.ui_project_execution_mode = "beads"
    # We'll need this to resolve the beads repo
    ctrl.ui_files_base_dir = str(workspace_dir)
    
    # 4. Call the new loading method (to be implemented)
    # For now, we simulate what we expect to happen
    if hasattr(ctrl, "_load_active_tickets"):
        ctrl._load_active_tickets()
    else:
        # Initial implementation will go here or in init_state
        pass
        
    # 5. Verify active_tickets populated from Beads
    assert len(ctrl.active_tickets) == 1
    assert ctrl.active_tickets[0]["id"] == "bead-1"
    assert ctrl.active_tickets[0]["description"] == "Description 1"
