"""Tests for verification and anchor endpoints."""

from __future__ import annotations

from ads_foundation.prooftrail import ProofTrailMonitor, _receipt_to_submission


def _make_receipt_data():
    """Create a valid receipt using the SDK and return submission dict + receipt."""
    monitor = ProofTrailMonitor()

    @monitor.track_tool_call
    def dummy(x):
        return x * 2

    with monitor.session(agent_id="test") as session:
        dummy(42)
        receipt = session.get_latest_receipt()

    return _receipt_to_submission(receipt), receipt


class TestVerify:
    def test_valid_proof(self, client, auth_headers):
        sub, _ = _make_receipt_data()
        resp = client.post(
            "/verify",
            json={
                "record_hash": sub["record_hash"],
                "proof": sub["proof"],
                "proof_root": sub["proof_root"],
            },
            headers=auth_headers,
        )
        assert resp.status_code == 200
        data = resp.json()
        assert data["valid"] is True
        assert data["anchored"] is False

    def test_tampered_proof(self, client, auth_headers):
        sub, _ = _make_receipt_data()
        resp = client.post(
            "/verify",
            json={
                "record_hash": "bb" * 32,
                "proof": sub["proof"],
                "proof_root": sub["proof_root"],
            },
            headers=auth_headers,
        )
        assert resp.status_code == 200
        assert resp.json()["valid"] is False

    def test_anchored_check(self, client, auth_headers):
        sub, receipt = _make_receipt_data()

        # Create an anchor
        client.post(
            "/anchors",
            json={
                "root_hash": sub["proof_root"],
                "receipt_count": 1,
            },
            headers=auth_headers,
        )

        # Verify -- should be anchored
        resp = client.post(
            "/verify",
            json={
                "record_hash": sub["record_hash"],
                "proof": sub["proof"],
                "proof_root": sub["proof_root"],
            },
            headers=auth_headers,
        )
        data = resp.json()
        assert data["valid"] is True
        assert data["anchored"] is True
        assert data["anchor_created_at"] is not None


class TestAnchors:
    def test_create_anchor(self, client, auth_headers):
        resp = client.post(
            "/anchors",
            json={"root_hash": "cc" * 32, "receipt_count": 5},
            headers=auth_headers,
        )
        assert resp.status_code == 201
        data = resp.json()
        assert data["root_hash"] == "cc" * 32
        assert data["receipt_count"] == 5

    def test_duplicate_anchor(self, client, auth_headers):
        payload = {"root_hash": "dd" * 32, "receipt_count": 3}
        client.post("/anchors", json=payload, headers=auth_headers)

        resp = client.post("/anchors", json=payload, headers=auth_headers)
        assert resp.status_code == 409
