#!/usr/bin/env python3
"""Smoke testy pro CyberBrain26 API (Fáze 7)."""

import httpx
import sys

BASE_URL = "http://localhost:8080"

def test_list_workspaces():
    r = httpx.get(f"{BASE_URL}/workspaces")
    assert r.status_code == 200
    data = r.json()
    assert isinstance(data, list)
    print("✅ test_list_workspaces")

def test_create_workspace():
    r = httpx.post(
        f"{BASE_URL}/workspaces",
        json={"name": "Smoke Test WS", "description": "Test", "instructions": "Test instrukce"}
    )
    assert r.status_code == 200
    ws = r.json()
    assert ws.get("name") == "Smoke Test WS"
    assert "id" in ws
    print("✅ test_create_workspace")
    return ws["id"]

def test_update_instructions(ws_id: str):
    r = httpx.put(
        f"{BASE_URL}/workspaces/{ws_id}/instructions",
        json={"instructions": "Nové instrukce"}
    )
    assert r.status_code == 200
    data = r.json()
    assert data.get("instructions") == "Nové instrukce"
    print("✅ test_update_instructions")

def test_rename_workspace(ws_id: str):
    r = httpx.put(
        f"{BASE_URL}/workspaces/{ws_id}/rename",
        json={"name": "Přejmenovaný Smoke Test", "description": "Po přejmenování"}
    )
    assert r.status_code == 200
    data = r.json()
    assert data.get("name") == "Přejmenovaný Smoke Test"
    print("✅ test_rename_workspace")

def test_archive_workspace(ws_id: str):
    r = httpx.put(
        f"{BASE_URL}/workspaces/{ws_id}/archive",
        json={"archived": True}
    )
    assert r.status_code == 200
    data = r.json()
    assert data.get("archived") is True
    print("✅ test_archive_workspace")

def test_list_conversations():
    r = httpx.get(f"{BASE_URL}/conversations")
    assert r.status_code == 200
    data = r.json()
    assert isinstance(data, list)
    print("✅ test_list_conversations")

def test_bulk_archive_conversations():
    # Nejprve získáme seznam konverzací
    r = httpx.get(f"{BASE_URL}/conversations")
    assert r.status_code == 200
    conversations = r.json()
    if not conversations:
        print("⚠️  test_bulk_archive_conversations: žádné konverzace k archivaci")
        return
    # Archivujeme první konverzaci (pokud existuje)
    conv_id = conversations[0]["id"]
    r = httpx.post(
        f"{BASE_URL}/conversations/bulk-archive",
        json={"ids": [conv_id]}
    )
    assert r.status_code == 200
    data = r.json()
    assert "archived_count" in data
    print("✅ test_bulk_archive_conversations")

def main():
    try:
        # Test prostorů
        test_list_workspaces()
        ws_id = test_create_workspace()
        test_update_instructions(ws_id)
        test_rename_workspace(ws_id)
        test_archive_workspace(ws_id)

        # Test konverzací
        test_list_conversations()
        test_bulk_archive_conversations()

        print("\n🎉 Všechny smoke testy prošly!")
        return 0
    except AssertionError as e:
        print(f"\n❌ Test selhal: {e}")
        return 1
    except httpx.HTTPError as e:
        print(f"\n❌ HTTP chyba: {e}")
        return 1
    except Exception as e:
        print(f"\n❌ Neočekávaná chyba: {e}")
        return 1

if __name__ == "__main__":
    sys.exit(main())
