"""Tests for runtime.integrations.webhooks.WebhookRegistry."""

import uuid

from runtime.integrations.webhooks import WebhookRegistry


def test_register_creates_webhook(tmp_path):
    """register() returns a valid UUID and the webhook appears in list_all()."""
    registry = WebhookRegistry(tmp_path)

    webhook_id = registry.register("test", "chain1", "secret")

    # Returned value is a valid UUID4 string
    parsed = uuid.UUID(webhook_id, version=4)
    assert str(parsed) == webhook_id

    # The webhook shows up in list_all()
    all_webhooks = registry.list_all()
    assert len(all_webhooks) == 1
    wh = all_webhooks[0]
    assert wh["id"] == webhook_id
    assert wh["name"] == "test"
    assert wh["chain_template"] == "chain1"
    assert "auth_token_hash" not in wh


def test_list_all_excludes_auth_hash(tmp_path):
    """list_all() must never expose auth_token_hash or the raw token."""
    registry = WebhookRegistry(tmp_path)
    raw_token = "super-secret-token-12345"

    registry.register("hook1", "chain_a", raw_token)

    all_webhooks = registry.list_all()
    assert len(all_webhooks) == 1

    wh = all_webhooks[0]
    # The hash key must be stripped from the dict
    assert "auth_token_hash" not in wh
    # The raw token must not appear anywhere in the serialised output
    serialized = str(all_webhooks)
    assert raw_token not in serialized


def test_validate_auth_correct_token(tmp_path):
    """validate_auth() returns True when the provided token matches."""
    registry = WebhookRegistry(tmp_path)
    token = "correct-secret-token"

    webhook_id = registry.register("auth_test", "chain1", token)

    assert registry.validate_auth(webhook_id, token) is True


def test_validate_auth_wrong_token(tmp_path):
    """validate_auth() returns False when the provided token does not match."""
    registry = WebhookRegistry(tmp_path)

    webhook_id = registry.register("auth_test", "chain1", "real-token")

    assert registry.validate_auth(webhook_id, "wrong-token") is False


def test_delete_removes_webhook(tmp_path):
    """register() then delete() returns True and list_all() is empty."""
    registry = WebhookRegistry(tmp_path)

    webhook_id = registry.register("to_delete", "chain_x", "token123")

    result = registry.delete(webhook_id)

    assert result is True
    assert registry.list_all() == []


def test_log_trigger_and_get_logs(tmp_path):
    """log_trigger() stores entries and get_logs() returns them newest-first."""
    registry = WebhookRegistry(tmp_path)
    webhook_id = registry.register("log_test", "chain1", "token")

    registry.log_trigger(webhook_id, "success", 100.0)
    registry.log_trigger(webhook_id, "error", 250.5, error="timeout")
    registry.log_trigger(webhook_id, "success", 50.0)

    logs = registry.get_logs(webhook_id)

    assert len(logs) == 3
    # Reverse chronological: timestamps must be non-increasing
    for i in range(len(logs) - 1):
        assert logs[i]["timestamp"] >= logs[i + 1]["timestamp"]
    # Verify content of each log entry
    assert all(log["webhook_id"] == webhook_id for log in logs)
    statuses = [log["status"] for log in logs]
    assert "success" in statuses
    assert "error" in statuses
