"""Tests for dataclass deserialisation in types.py."""

from sentryagent_idp.types import (
    Agent,
    Credential,
    CredentialWithSecret,
    PaginatedAgents,
    PaginatedCredentials,
    TokenResponse,
    IntrospectResponse,
    AuditEvent,
    PaginatedAuditEvents,
    RegisterAgentRequest,
    UpdateAgentRequest,
)


AGENT_DICT = {
    "agentId": "uuid-1",
    "email": "a@b.ai",
    "agentType": "screener",
    "version": "1.0.0",
    "capabilities": ["read"],
    "owner": "team",
    "deploymentEnv": "production",
    "status": "active",
    "createdAt": "2026-01-01T00:00:00Z",
    "updatedAt": "2026-01-02T00:00:00Z",
}

CREDENTIAL_DICT = {
    "credentialId": "cred-1",
    "clientId": "uuid-1",
    "status": "active",
    "createdAt": "2026-01-01T00:00:00Z",
    "expiresAt": None,
    "revokedAt": None,
}


def test_agent_from_dict() -> None:
    agent = Agent.from_dict(AGENT_DICT)
    assert agent.agent_id == "uuid-1"
    assert agent.agent_type == "screener"
    assert agent.capabilities == ["read"]


def test_register_agent_request_to_dict() -> None:
    req = RegisterAgentRequest(
        email="a@b.ai",
        agent_type="classifier",
        version="1.0.0",
        capabilities=["read"],
        owner="team",
        deployment_env="production",
    )
    d = req.to_dict()
    assert d["agentType"] == "classifier"
    assert d["deploymentEnv"] == "production"


def test_update_agent_request_omits_none() -> None:
    req = UpdateAgentRequest(version="2.0.0")
    d = req.to_dict()
    assert "version" in d
    assert "agentType" not in d


def test_paginated_agents_from_dict() -> None:
    result = PaginatedAgents.from_dict({"data": [AGENT_DICT], "total": 1, "page": 1, "limit": 20})
    assert result.total == 1
    assert result.data[0].agent_id == "uuid-1"


def test_credential_from_dict() -> None:
    cred = Credential.from_dict(CREDENTIAL_DICT)
    assert cred.credential_id == "cred-1"
    assert cred.expires_at is None


def test_credential_with_secret_from_dict() -> None:
    d = {**CREDENTIAL_DICT, "clientSecret": "sk_live_abc"}
    cred = CredentialWithSecret.from_dict(d)
    assert cred.client_secret == "sk_live_abc"
    assert cred.credential_id == "cred-1"


def test_paginated_credentials_from_dict() -> None:
    result = PaginatedCredentials.from_dict(
        {"data": [CREDENTIAL_DICT], "total": 1, "page": 1, "limit": 20}
    )
    assert result.total == 1


def test_token_response_from_dict() -> None:
    tr = TokenResponse.from_dict(
        {"access_token": "tok", "token_type": "Bearer", "expires_in": 3600, "scope": "agents:read"}
    )
    assert tr.access_token == "tok"
    assert tr.expires_in == 3600


def test_introspect_response_active() -> None:
    ir = IntrospectResponse.from_dict({"active": True, "sub": "uuid-1", "exp": 9999999999})
    assert ir.active is True
    assert ir.sub == "uuid-1"


def test_introspect_response_inactive() -> None:
    ir = IntrospectResponse.from_dict({"active": False})
    assert ir.active is False
    assert ir.sub is None


def test_audit_event_from_dict() -> None:
    ev = AuditEvent.from_dict({
        "eventId": "ev-1", "agentId": "uuid-1", "action": "token.issued",
        "outcome": "success", "ipAddress": "1.2.3.4", "userAgent": "curl",
        "metadata": {}, "timestamp": "2026-01-01T00:00:00Z",
    })
    assert ev.event_id == "ev-1"
    assert ev.action == "token.issued"


def test_paginated_audit_events_from_dict() -> None:
    ev_dict = {
        "eventId": "ev-1", "agentId": "uuid-1", "action": "token.issued",
        "outcome": "success", "ipAddress": "1.2.3.4", "userAgent": "curl",
        "metadata": {}, "timestamp": "2026-01-01T00:00:00Z",
    }
    result = PaginatedAuditEvents.from_dict({"data": [ev_dict], "total": 1, "page": 1, "limit": 20})
    assert result.total == 1
    assert result.data[0].event_id == "ev-1"
