"""Behavioral checks: failures must not masquerade as successful delivery."""
from __future__ import annotations

import asyncio
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
import threading
import time

import pytest
from fastapi.testclient import TestClient

from log_capstone.app import Principal, Settings, create_app
from log_capstone.contracts import ConflictError, ImportInput, ImportResult
from log_capstone.queueing import CapacityError, ImportQueue
from log_capstone.storage import Base, SqlStore
from sqlalchemy import create_engine, event

TOKEN_A = "teaching-token-owner-a"
TOKEN_B = "teaching-token-owner-b"
ADMIN = "teaching-admin-token"
PAYLOAD = {"records": [{"source": "api", "timestamp": "2026-10-29T12:00:00+08:00",
                        "level": "INFO", "message": "test"}]}


def headers(token: str = TOKEN_A, key: str = "batch-1") -> dict[str, str]:
    return {"Authorization": "Bearer " + token, "Idempotency-Key": key}


def settings(path: Path, timeout: float = 1.0) -> Settings:
    return Settings(path, {TOKEN_A: Principal("a"), TOKEN_B: Principal("b"),
                           ADMIN: Principal("ops", admin=True)}, response_timeout=timeout)


def test_api_idempotency_authorization_and_limits(tmp_path: Path) -> None:
    with TestClient(create_app(settings(tmp_path / "api.sqlite"))) as client:
        assert client.get("/health").status_code == 200
        assert client.post("/imports", json=PAYLOAD, headers={"Idempotency-Key": "x"}).status_code == 401
        first = client.post("/imports", json=PAYLOAD, headers=headers())
        second = client.post("/imports", json=PAYLOAD, headers=headers())
        assert first.status_code == second.status_code == 200
        assert first.json() == second.json()
        assert len(client.get("/records", headers=headers()).json()) == 1
        assert client.get("/records", headers=headers(TOKEN_B)).json() == []
        different = {"records": [{**PAYLOAD["records"][0], "message": "changed"}]}
        assert client.post("/imports", json=different, headers=headers()).status_code == 409
        assert client.post("/imports", json=PAYLOAD, headers=headers(TOKEN_B)).status_code == 200
        assert len(client.get("/records", headers=headers(TOKEN_B)).json()) == 1
        assert client.get("/records?limit=100000", headers=headers()).status_code == 422
        assert client.get("/metrics", headers=headers()).status_code == 403
        assert client.get("/metrics", headers=headers(ADMIN)).status_code == 200
        assert client.post("/imports", content=b"x" * 65537, headers=headers()).status_code == 413
        naive = {"records": [{**PAYLOAD["records"][0], "timestamp": "2026-10-29T12:00:00"}]}
        assert client.post("/imports", json=naive, headers=headers(key="naive")).status_code == 422
        row = client.get("/records", headers=headers()).json()[0]
        assert row["timestamp"] == "2026-10-29T04:00:00Z"


def test_concurrent_retries_and_schema_reopen(tmp_path: Path) -> None:
    database = tmp_path / "race.sqlite"
    store = SqlStore(database)
    payload = ImportInput.model_validate(PAYLOAD)
    both_read = threading.Barrier(2)
    observed_lock = threading.Lock()
    observed = 0
    def pause_after_read(connection: object, cursor: object, statement: str,
                         parameters: object, context: object, many: bool) -> None:
        nonlocal observed
        if statement.startswith("SELECT operations."):
            with observed_lock:
                observed += 1
                initial = observed <= 2
            if initial:
                both_read.wait(timeout=2)
    event.listen(store.engine, "after_cursor_execute", pause_after_read)
    try:
        with ThreadPoolExecutor(max_workers=2) as pool:
            results = list(pool.map(lambda _: store.ingest("a", "same", payload), range(2)))
        assert results[0] == results[1]
        event.remove(store.engine, "after_cursor_execute", pause_after_read)
        assert len(store.records("a", 0, 100)) == 1
        with store.engine.connect() as connection:
            assert connection.exec_driver_sql("PRAGMA user_version").scalar_one() == 2
            plan = connection.exec_driver_sql(
                "EXPLAIN QUERY PLAN SELECT * FROM records WHERE owner='a' AND id>0 ORDER BY id LIMIT 20").all()
            assert any("ix_records_owner_id" in str(row) for row in plan)
    finally:
        store.close()
    reopened = SqlStore(database)
    try:
        assert reopened.ingest("a", "same", payload) == results[0]
    finally:
        reopened.close()


class GatedStore:
    def __init__(self) -> None:
        self.started = threading.Event()
        self.release = threading.Event()
        self.calls = 0
        self.closed = False

    def ingest(self, owner: str, key: str, payload: ImportInput) -> ImportResult:
        self.started.set()
        if not self.release.wait(3):
            raise TimeoutError("test failed to release worker")
        self.calls += 1
        if key == "fail":
            raise RuntimeError("injected failure")
        return ImportResult(operation_id=key, count=len(payload.records))

    def records(self, owner: str, after: int, limit: int) -> list[dict[str, object]]:
        return []

    def close(self) -> None:
        self.closed = True


def test_queue_bounds_timeout_failure_and_drain() -> None:
    async def exercise() -> None:
        store = GatedStore()
        queue = ImportQueue(store, capacity=1)
        await queue.start()
        payload = ImportInput.model_validate(PAYLOAD)
        first = queue.submit("a", "first", payload)
        assert await asyncio.to_thread(store.started.wait, 1)
        second = queue.submit("a", "fail", payload)
        with pytest.raises(CapacityError):
            queue.submit("a", "rejected", payload)
        with pytest.raises(TimeoutError):
            await asyncio.wait_for(asyncio.shield(first), 0.01)
        assert not first.cancelled()
        closing = asyncio.create_task(queue.close())
        await asyncio.sleep(0)  # allow close to revoke admission; progress uses events below.
        with pytest.raises(CapacityError):
            queue.submit("a", "during-close", payload)
        assert not closing.done() and not store.closed
        store.release.set()
        assert (await first).operation_id == "first"
        with pytest.raises(RuntimeError, match="injected"):
            await second
        await asyncio.wait_for(closing, 1)
        assert queue.completed == 1 and queue.failed == 1
        assert store.closed and queue.task is not None and queue.task.done()
        with pytest.raises(CapacityError):
            queue.submit("a", "late", payload)
    asyncio.run(exercise())


def test_cancelled_waiter_does_not_kill_consumer() -> None:
    async def exercise() -> None:
        store = GatedStore()
        queue = ImportQueue(store, capacity=1)
        await queue.start()
        payload = ImportInput.model_validate(PAYLOAD)
        cancelled = queue.submit("a", "cancelled", payload)
        assert await asyncio.to_thread(store.started.wait, 1)
        cancelled.cancel()
        store.release.set()
        next_result = queue.submit("a", "next", payload)
        assert (await asyncio.wait_for(next_result, 1)).operation_id == "next"
        await queue.close()
        assert store.calls == 2 and queue.completed == 2 and store.closed
    asyncio.run(exercise())


def test_storage_failure_retains_request_identity(tmp_path: Path) -> None:
    class BrokenStore(GatedStore):
        def records(self, owner: str, after: int, limit: int) -> list[dict[str, object]]:
            raise RuntimeError("private storage details")
    store = BrokenStore()
    with TestClient(create_app(settings(tmp_path / "unused.sqlite"), store)) as client:
        response = client.get("/records", headers={**headers(), "X-Request-ID": "probe_1"})
        assert response.status_code == 503
        assert response.headers["X-Request-ID"] == "probe_1"
        assert "private" not in response.text


def test_http_timeout_can_commit_and_retry(tmp_path: Path) -> None:
    class SlowStore(SqlStore):
        def ingest(self, owner: str, key: str, payload: ImportInput) -> ImportResult:
            time.sleep(0.03)
            return super().ingest(owner, key, payload)

    store = SlowStore(tmp_path / "slow.sqlite")
    with TestClient(create_app(settings(tmp_path / "unused.sqlite", timeout=0.005), store)) as client:
        response = client.post("/imports", json=PAYLOAD, headers=headers())
        assert response.status_code == 504
        # Observe completion rather than treating HTTP timeout as transaction rollback.
        deadline = time.monotonic() + 2
        while time.monotonic() < deadline:
            if len(store.records("a", 0, 10)) == 1:
                break
            time.sleep(0.01)
        assert len(store.records("a", 0, 10)) == 1
        assert client.post("/imports", json=PAYLOAD, headers=headers()).status_code == 504
    reopened = SqlStore(tmp_path / "slow.sqlite")
    try:
        assert len(reopened.records("a", 0, 10)) == 1
    finally:
        reopened.close()


def test_contract_rejects_extra_fields_and_empty_batch() -> None:
    from pydantic import ValidationError
    with pytest.raises(ValidationError):
        ImportInput.model_validate({"records": []})
    with pytest.raises(ValidationError):
        ImportInput.model_validate({**PAYLOAD, "owner": "other-tenant"})


def test_stored_schema_one_upgrades_preserving_data(tmp_path: Path) -> None:
    database = tmp_path / "v1.sqlite"
    old = create_engine("sqlite:///" + str(database))
    try:
        with old.begin() as connection:
            Base.metadata.create_all(connection)
            connection.exec_driver_sql("PRAGMA user_version=1")
            connection.exec_driver_sql("INSERT INTO operations VALUES('a','old','hash','op-old',1)")
            connection.exec_driver_sql(
                "INSERT INTO records(owner,operation_id,source,timestamp,level,message) "
                "VALUES('a','op-old','v1','2026-10-29 00:00:00','INFO','preserve')")
    finally:
        old.dispose()
    upgraded = SqlStore(database)
    try:
        assert upgraded.records("a", 0, 20)[0]["message"] == "preserve"
        with upgraded.engine.connect() as connection:
            assert connection.exec_driver_sql("PRAGMA user_version").scalar_one() == 2
            indexes = connection.exec_driver_sql("PRAGMA index_list(records)").all()
            assert any(row[1] == "ix_records_owner_id" for row in indexes)
    finally:
        upgraded.close()


def test_payload_and_key_contract_are_stable() -> None:
    from log_capstone.contracts import validate_operation_key
    batch = ImportInput.model_validate(PAYLOAD)
    assert isinstance(batch.records, tuple)
    assert validate_operation_key("valid_key-1") == "valid_key-1"
    with pytest.raises(ValueError):
        validate_operation_key("cannot replay via HTTP")
