"""Utils Views"""

# Django
from django.contrib.contenttypes.models import ContentType
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views.decorators.cache import cache_page

# DRF
from rest_framework.response import Response
from rest_framework.views import APIView


class ContentTypesViewSet(APIView):
    """Content Types list API view"""

    @method_decorator(cache_page(60 * 60 * 2))
    def get(self, request, *args, **kwargs):
        content_types = ContentType.objects.filter(
            model__in=[
                "account",
                "vehicle",
                "user",
                "parking",
                "level",
                "accesspoint",
                "fare",
                "device",
                "farecategory",
                "company",
                "devicetype",
                "mediatype",
                "roundingfactor",
                "ticket",
                "cashregister",
                "plan",
            ]
        ).values("id", "model")

        model_to_id = {ct["model"]: ct["id"] for ct in content_types}

        structure = [
            ("account", None),
            ("vehicle", "account"),
            ("user", None),
            ("parking", None),
            ("level", "parking"),
            ("cashregister", "parking"),
            ("accesspoint", "parking"),
            ("ticket", "parking"),
            ("fare", "parking"),
            ("device", "parking"),
            ("plan", "parking"),
            ("farecategory", None),
            ("company", None),
            ("devicetype", None),
            ("mediatype", None),
        ]

        response_data = []
        for model, parent in structure:
            item = {"id": model_to_id[model], "name": _(model)}
            if parent:
                item["parent"] = model_to_id[parent]
            response_data.append(item)

        return Response(response_data)
