from datetime import datetime
from datetime import timezone

from a2a_agent.config import AgentConfig
from a2a_agent.config import BrokerConfig
from a2a_agent.config import PendingPairing
from a2a_agent.config import ShareConfig
from a2a_agent.pairing import add_pending_pairing
from a2a_agent.pairing import consume_pending_pairing


NOW = datetime(2026, 7, 8, 12, 0, tzinfo=timezone.utc)


def test_add_pending_pairing_records_device_room_and_expiry() -> None:
    config = AgentConfig(room="team", role="commander")

    code, updated = add_pending_pairing(config, "macbook-air", now=NOW)

    assert len(code) == 6
    assert updated.pending_pairings[code].device_name == "macbook-air"
    assert updated.pending_pairings[code].room == "team"
    assert updated.pending_pairings[code].expires_at == "2026-07-08T12:10:00Z"


def test_consume_pending_pairing_returns_settings_and_removes_code() -> None:
    config = AgentConfig(
        room="team",
        role="commander",
        broker=BrokerConfig(host="broker.example.com", port=1883, tls=False),
        pending_pairings={
            "ABC234": PendingPairing(
                device_name="macbook-air",
                room="team",
                expires_at="2026-07-08T12:10:00Z",
            )
        },
    )

    reply, updated = consume_pending_pairing(
        config,
        "ABC234",
        now=NOW,
        token_factory=lambda: "fixed-token",
        share=ShareConfig(
            host="nas.example.com",
            port=5001,
            user="a2a-worker",
            password="secret",
            folder="/a2a-share",
            insecure=True,
        ),
    )

    assert reply.ok is True
    assert reply.room == "team"
    assert reply.device_name == "macbook-air"
    assert reply.token == "fixed-token"
    assert reply.broker == {"host": "broker.example.com", "port": 1883, "tls": False}
    assert reply.share == {
        "host": "nas.example.com",
        "port": 5001,
        "user": "a2a-worker",
        "password": "secret",
        "folder": "/a2a-share",
        "insecure": True,
    }
    assert updated.pending_pairings == {}


def test_consume_pending_pairing_rejects_missing_or_expired_code() -> None:
    missing_reply, missing_config = consume_pending_pairing(
        AgentConfig(role="commander"),
        "ABC234",
        now=NOW,
    )
    assert missing_reply.ok is False
    assert missing_reply.error == "pairing code not found"
    assert missing_config.pending_pairings == {}

    expired_config = AgentConfig(
        role="commander",
        pending_pairings={
            "ABC234": PendingPairing(
                device_name="macbook-air",
                room="team",
                expires_at="2026-07-08T11:59:59Z",
            )
        },
    )
    expired_reply, expired_updated = consume_pending_pairing(
        expired_config,
        "ABC234",
        now=NOW,
    )

    assert expired_reply.ok is False
    assert expired_reply.error == "pairing code expired"
    assert expired_updated.pending_pairings == {}
