from rest_framework import status
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.account.models import User
from apps.master import models as master_models
from apps.station import models as station_models


class DashboardCountAPIView(APIView):
    """
    A view for retrieving counts of various entities for the dashboard.

    Permissions:
    - Requires the user to be authenticated and have admin privileges.

    HTTP Methods:
    - GET: Retrieves counts of stations, amenities, connectors, locations,
           network operators, and users.

    Returns:
    A JSON response with counts of various entities.
    """

    permission_classes = (IsAuthenticated, IsAdminUser)
    http_method_names = ["get"]

    def get(self, request, *args, **kwargs):
        # Retrieve counts for various entities
        stations = (
            station_models.StationMaster.objects.filter(is_verified=True)
            .annotate_is_rejected_station()
            .filter(is_rejected=False)
        )
        users = User.objects.all()
        amenities = master_models.AmenitiesMaster.objects.all()
        connectors = master_models.ConnectorMaster.objects.all()
        locations = master_models.LocationMaster.objects.all()
        network_operators = master_models.NetworkOperatorMaster.objects.all()

        # Create a response dictionary with entity counts
        response = {
            "stations": stations.count(),
            "amenities": amenities.count(),
            "connectors": connectors.count(),
            "locations": locations.count(),
            "network_operators": network_operators.count(),
            "users": users.count(),
        }

        # Return the response with a 200 status code
        return Response(response, status=status.HTTP_200_OK)
