"""Shared pytest fixtures for SDK tests."""

from __future__ import annotations

from datetime import datetime, timezone
from typing import Any
from unittest.mock import MagicMock

import pytest
import requests


@pytest.fixture
def mock_session() -> MagicMock:
    """Create a mock requests.Session with headers dict."""
    session = MagicMock(spec=requests.Session)
    session.headers = {}
    return session


@pytest.fixture
def mock_response_factory():
    """Factory fixture for creating mock Response objects."""

    def _create(
        *,
        status_code: int = 200,
        json_data: Any | None = None,
        text: str = "mock-response",
    ) -> MagicMock:
        response = MagicMock(spec=requests.Response)
        response.status_code = status_code
        response.ok = status_code < 400
        response.text = text
        response.json.return_value = json_data
        return response

    return _create


@pytest.fixture
def auth_payload() -> dict:
    """Standard authentication response payload."""
    return {
        "access_token": "token123",
        "expires_at": "2099-01-01T00:00:00+00:00",
        "user": {
            "id": "user-1",
            "agent_name": "agent",
            "display_name": "Agent",
            "mail_address": "agent@example.com",
            "email": "agent@example.com",
            "email_verified": True,
            "email_bound_at": "2024-01-01T00:00:00+00:00",
            "is_active": True,
            "created_at": "2024-01-01T00:00:00+00:00",
            "updated_at": "2024-01-01T00:00:00+00:00",
        },
    }


@pytest.fixture
def identity_payload() -> dict:
    """Standard identity response payload."""
    return {
        "id": "cirtusai:user-1",
        "name": "Agent",
        "type": "personal",
        "status": "active",
        "create_time": "2024-01-01T00:00:00+00:00",
        "last_active_at": "2024-01-02T00:00:00+00:00",
        "creator": {
            "id": "org:cirtusai",
            "name": "CirtusAI",
            "auth_provider": "CirtusAI",
        },
        "assets": [
            {
                "id": "asset:mail:agent",
                "address": "agent@example.com",
                "type": "mail",
                "status": "connected",
                "scopes": ["read", "send"],
            }
        ],
        "linked_platforms": [
            {
                "name": "Cirtus Mail Bridge",
                "connection_status": "connected",
                "connection_type": "oauth2",
            }
        ],
        "credentials": {
            "issuer": "CirtusAI",
            "signature_key": "ed25519:1234abcd",
            "access_token": "cai_token",
            "expires_at": "2099-02-01T00:00:00+00:00",
        },
        "metadata": {"description": "Agent identity", "tags": ["agent"]},
    }


@pytest.fixture
def future_expiry() -> datetime:
    """Return a datetime far in the future for token expiry tests."""
    return datetime.now(timezone.utc).replace(year=2099)
