import os
import httpx
import pytest
import time
from conftest import BASE_URL

def _headers():
    uid = os.getenv("SYNC_TEST_USER_ID", "")
    return {"X-User-ID": uid}

def test_skill_lifecycle():
    """
    Test creating, listing, updating files, and deleting a skill.
    """
    import uuid
    skill_data = {
        "name": f"Test Skill Extended {uuid.uuid4().hex[:8]}",
        "description": "A test skill for integration testing",
        "features": ["test_feature"]
    }
    
    with httpx.Client(timeout=10.0) as client:
        # 1. Create Skill
        r = client.post(f"{BASE_URL}/skills/", json=skill_data, headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        json_data = r.json()
        assert "id" in json_data
        skill_id = json_data["id"]
        
        # 2. List Skills
        r = client.get(f"{BASE_URL}/skills/", headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        skills = r.json()
        assert len(skills) > 0
        
        # Find our skill
        test_skill = None
        for s in skills:
            if s.get("id") == skill_id:
                test_skill = s
                break
                
        assert test_skill is not None, "Created skill not found in list"
        
        # 2.5 Update Skill
        r = client.put(f"{BASE_URL}/skills/{skill_id}", json={"extra_metadata": {"emoji": "🚀"}}, headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        # 3. List Files
        r = client.get(f"{BASE_URL}/skills/{skill_id}/files", headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        files = r.json()
        assert len(files) > 0
        assert any(f["path"] == "SKILL.md" for f in files)
        
        # 4. Create a file in the skill
        file_path = "test_file.txt"
        file_content = "Hello Skill File!"
        r = client.post(f"{BASE_URL}/skills/{skill_id}/files/{file_path}", json={"content": file_content}, headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        # 5. Read the file
        r = client.get(f"{BASE_URL}/skills/{skill_id}/files/{file_path}", headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        assert r.json()["content"] == file_content
        
        # 6. Delete the file
        r = client.delete(f"{BASE_URL}/skills/{skill_id}/files/{file_path}", headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        # Verify file is gone
        r = client.get(f"{BASE_URL}/skills/{skill_id}/files/{file_path}", headers=_headers())
        assert r.status_code == 404
        
        # 7. Delete the skill
        r = client.delete(f"{BASE_URL}/skills/{skill_id}", headers=_headers())
        assert r.status_code == 200, f"Expected 200 OK, got {r.status_code}: {r.text}"
        
        # Verify skill is gone
        r = client.get(f"{BASE_URL}/skills/", headers=_headers())
        skills = r.json()
        assert not any(s.get("id") == skill_id for s in skills)
