from rest_framework import viewsets, mixins
from rest_framework.permissions import IsAdminUser, AllowAny
from drf_spectacular.utils import extend_schema, extend_schema_view
from .models import SiteConfiguration
from .serializers import (
	SiteConfigurationAdminSerializer,
	SiteConfigurationPublicSerializer,
)


class _SingletonQuerysetMixin:
	def get_queryset(self):
		return SiteConfiguration.objects.filter(pk=1)

	def get_object(self):
		return SiteConfiguration.get_solo()


@extend_schema_view(
	list=extend_schema(tags=["configuration"], summary="List site configuration (admin)"),
	retrieve=extend_schema(tags=["configuration"], summary="Retrieve site configuration (admin)"),
	create=extend_schema(tags=["configuration"], summary="Create site configuration (admin)"),
	partial_update=extend_schema(tags=["configuration"], summary="Update site configuration (admin)"),
	update=extend_schema(tags=["configuration"], summary="Replace site configuration (admin)"),
	destroy=extend_schema(tags=["configuration"], summary="Delete site configuration (admin)"),
)
class SiteConfigurationAdminViewSet(_SingletonQuerysetMixin, viewsets.ModelViewSet):
	permission_classes = [IsAdminUser]
	serializer_class = SiteConfigurationAdminSerializer


@extend_schema_view(
	list=extend_schema(tags=["configuration", "public"], summary="List site configuration (public)"),
	retrieve=extend_schema(tags=["configuration", "public"], summary="Retrieve site configuration (public)"),
)
class SiteConfigurationPublicViewSet(_SingletonQuerysetMixin, viewsets.ReadOnlyModelViewSet):
	permission_classes = [AllowAny]
	serializer_class = SiteConfigurationPublicSerializer