import json
import threading
import time
import uuid

import pytest
import sseclient


def _ai_config_video() -> dict:
    return {}


def _ai_config_image() -> dict:
    return {}


def test_ft_p08_immediate_async_response(
    warm_engine, http_client, jwt_token
):
    media_id = f"async-{uuid.uuid4().hex}"
    body = _ai_config_image()
    channel_id = str(uuid.uuid4())
    headers = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": channel_id}
    t0 = time.monotonic()
    r = http_client.post(f"/detect/{media_id}", json=body, headers=headers)
    elapsed = time.monotonic() - t0
    assert elapsed < 2.0
    assert r.status_code == 202


@pytest.mark.timeout(10)
def test_ft_p09_sse_event_delivery(
    warm_engine, http_client, jwt_token
):
    media_id = f"sse-{uuid.uuid4().hex}"
    channel_id = str(uuid.uuid4())
    body = _ai_config_video()
    auth_header = {"Authorization": f"Bearer {jwt_token}"}
    post_headers = {**auth_header, "X-Channel-Id": channel_id}
    collected: list[dict] = []
    thread_exc: list[BaseException] = []
    first_event = threading.Event()
    connected = threading.Event()

    def _listen():
        try:
            with http_client.get(
                f"/detect/events/{channel_id}",
                stream=True,
                timeout=600,
                headers=auth_header,
            ) as resp:
                resp.raise_for_status()
                connected.set()
                for event in sseclient.SSEClient(resp).events():
                    if not event.data or not str(event.data).strip():
                        continue
                    data = json.loads(event.data)
                    collected.append(data)
                    first_event.set()
                    if len(collected) >= 5:
                        break
        except BaseException as e:
            thread_exc.append(e)
        finally:
            connected.set()
            first_event.set()

    th = threading.Thread(target=_listen, daemon=True)
    th.start()
    connected.wait(timeout=5)
    r = http_client.post(f"/detect/{media_id}", json=body, headers=post_headers)
    assert r.status_code == 202
    first_event.wait(timeout=5)
    th.join(timeout=5)
    assert not thread_exc, thread_exc
    assert collected, "no SSE events received"


def test_ft_n04_duplicate_media_id_409(
    warm_engine, http_client, jwt_token
):
    media_id = "dup-test"
    body = _ai_config_image()
    headers1 = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": str(uuid.uuid4())}
    headers2 = {"Authorization": f"Bearer {jwt_token}", "X-Channel-Id": str(uuid.uuid4())}
    r1 = http_client.post(f"/detect/{media_id}", json=body, headers=headers1)
    assert r1.status_code == 202
    r2 = http_client.post(f"/detect/{media_id}", json=body, headers=headers2)
    assert r2.status_code == 409
