from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from vbv_lernwelt.core.models import Country, Organisation
from vbv_lernwelt.core.serializers import CountrySerializer, OrganisationSerializer


@api_view(["GET"])
@permission_classes([IsAuthenticated])
def list_entities(request):
    language_code = request.LANGUAGE_CODE if request.LANGUAGE_CODE else "de"
    field_mapping = {
        "de": "name_de",
        "fr": "name_fr",
        "it": "name_it",
    }

    field_name = field_mapping.get(language_code, field_mapping["de"])

    context = {"langauge": request.user.language}

    organisations = OrganisationSerializer(
        Organisation.objects.all().order_by(field_name), many=True, context=context
    ).data
    countries = CountrySerializer(
        Country.objects.all(), many=True, context=context
    ).data
    return Response({"organisations": organisations, "countries": countries})
