import json

from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404

from apps.lyric.models import User


AP_CONTEXT = [
    "https://www.w3.org/ns/activitystreams",
    {"earthman": "https://earthmanrpg.me/ns#"},
]


def _base_url(request):
    return f"{request.scheme}://{request.get_host()}"


def _ap_response(data):
    return HttpResponse(
        json.dumps(data),
        content_type="application/activity+json",
    )


def webfinger(request):
    resource = request.GET.get("resource", "")
    if not resource:
        return HttpResponse(status=400)
    # Expect acct:username@host
    if not resource.startswith("acct:"):
        return HttpResponse(status=400)
    username = resource[len("acct:"):].split("@")[0]
    user = get_object_or_404(User, username=username)
    base = _base_url(request)
    data = {
        "subject": resource,
        "links": [
            {
                "rel": "self",
                "type": "application/activity+json",
                "href": f"{base}/ap/users/{user.username}/",
            }
        ],
    }
    return HttpResponse(json.dumps(data), content_type="application/jrd+json")


def actor(request, username):
    user = get_object_or_404(User, username=username)
    user.ensure_keypair()
    base = _base_url(request)
    actor_url = f"{base}/ap/users/{username}/"
    data = {
        "@context": AP_CONTEXT,
        "id": actor_url,
        "type": "Person",
        "preferredUsername": username,
        "inbox": f"{actor_url}inbox/",
        "outbox": f"{actor_url}outbox/",
        "publicKey": {
            "id": f"{actor_url}#main-key",
            "owner": actor_url,
            "publicKeyPem": user.ap_public_key,
        },
    }
    return _ap_response(data)


def outbox(request, username):
    user = get_object_or_404(User, username=username)
    base = _base_url(request)
    events = user.game_events.select_related("room").order_by("timestamp")
    activities = [a for e in events if (a := e.to_activity(base)) is not None]
    actor_url = f"{base}/ap/users/{username}/"
    data = {
        "@context": AP_CONTEXT,
        "id": f"{actor_url}outbox/",
        "type": "OrderedCollection",
        "totalItems": len(activities),
        "orderedItems": activities,
    }
    return _ap_response(data)
