from rest_framework import viewsets, status
from rest_framework import routers
from .models import (
    Role, Permission, RolePermission, User, Customer, Material, MaterialDetail, 
    Operation, Regulation, ComplianceCheck, Report, AuditLog, Authority, 
    SustainabilityTracking, SupportTicket
)
from .serializers import (
    RoleSerializer, PermissionSerializer, RolePermissionSerializer, UserSerializer, 
    CustomerSerializer, MaterialSerializer, MaterialDetailSerializer, OperationSerializer, 
    RegulationSerializer, ComplianceCheckSerializer, ReportSerializer, AuditLogSerializer, 
    AuthoritySerializer, SustainabilityTrackingSerializer, SupportTicketSerializer
)

from tracking.models import Vehicle, Trip, MaintenanceLog, Appointment, GPSData, FuelTransaction
from .serializers import VehicleSerializer, TripSerializer, MaintenanceLogSerializer, AppointmentSerializer,\
        GPSDataSerializer, FuelTransactionSerializer, UserProfileSerializer

from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action
from rest_framework.response import Response


from userprofile.models import UserProfile
# ViewSets define the view behavior.
class RoleViewSet(viewsets.ModelViewSet):
    queryset = Role.objects.all()
    serializer_class = RoleSerializer

class PermissionViewSet(viewsets.ModelViewSet):
    queryset = Permission.objects.all()
    serializer_class = PermissionSerializer

class RolePermissionViewSet(viewsets.ModelViewSet):
    queryset = RolePermission.objects.all()
    serializer_class = RolePermissionSerializer

class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

class CustomerViewSet(viewsets.ModelViewSet):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

class MaterialViewSet(viewsets.ModelViewSet):
    queryset = Material.objects.all()
    serializer_class = MaterialSerializer

class MaterialDetailViewSet(viewsets.ModelViewSet):
    queryset = MaterialDetail.objects.all()
    serializer_class = MaterialDetailSerializer

class OperationViewSet(viewsets.ModelViewSet):
    queryset = Operation.objects.all()
    serializer_class = OperationSerializer

class RegulationViewSet(viewsets.ModelViewSet):
    queryset = Regulation.objects.all()
    serializer_class = RegulationSerializer

class ComplianceCheckViewSet(viewsets.ModelViewSet):
    queryset = ComplianceCheck.objects.all()
    serializer_class = ComplianceCheckSerializer

class ReportViewSet(viewsets.ModelViewSet):
    queryset = Report.objects.all()
    serializer_class = ReportSerializer

class AuditLogViewSet(viewsets.ModelViewSet):
    queryset = AuditLog.objects.all()
    serializer_class = AuditLogSerializer

class AuthorityViewSet(viewsets.ModelViewSet):
    queryset = Authority.objects.all()
    serializer_class = AuthoritySerializer

class SustainabilityTrackingViewSet(viewsets.ModelViewSet):
    queryset = SustainabilityTracking.objects.all()
    serializer_class = SustainabilityTrackingSerializer

class SupportTicketViewSet(viewsets.ModelViewSet):
    queryset = SupportTicket.objects.all()
    serializer_class = SupportTicketSerializer


class VehicleViewSet(viewsets.ModelViewSet):
    queryset = Vehicle.objects.all()
    serializer_class = VehicleSerializer

class TripViewSet(viewsets.ModelViewSet):
    queryset = Trip.objects.all()
    serializer_class = TripSerializer

class MaintenanceLogViewSet(viewsets.ModelViewSet):
    queryset = MaintenanceLog.objects.all()
    serializer_class = MaintenanceLogSerializer

class AppointmentViewSet(viewsets.ModelViewSet):
    queryset = Appointment.objects.all()
    serializer_class = AppointmentSerializer

class GPSDataViewSet(viewsets.ModelViewSet):
    queryset = GPSData.objects.all()
    serializer_class = GPSDataSerializer

class FuelTransactionViewSet(viewsets.ModelViewSet):
    queryset = FuelTransaction.objects.all()
    serializer_class = FuelTransactionSerializer

class UserProfileViewSet(viewsets.ModelViewSet):
    queryset = UserProfile.objects.all()
    serializer_class = UserProfileSerializer
    permission_classes = [IsAuthenticated]

    @action(detail=False, methods=['get'], permission_classes=[IsAuthenticated])
    def myprofile(self, request):
        try:
            profile = UserProfile.objects.get(user=request.user)
            serializer = self.get_serializer(profile)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except UserProfile.DoesNotExist:
            return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND)


# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'roles', RoleViewSet)
router.register(r'permissions', PermissionViewSet)
router.register(r'role_permissions', RolePermissionViewSet)
router.register(r'users', UserViewSet)
router.register(r'customers', CustomerViewSet)
router.register(r'materials', MaterialViewSet)
router.register(r'material_details', MaterialDetailViewSet)
router.register(r'operations', OperationViewSet)
router.register(r'regulations', RegulationViewSet)
router.register(r'compliance_checks', ComplianceCheckViewSet)
router.register(r'reports', ReportViewSet)
router.register(r'audit_logs', AuditLogViewSet)
router.register(r'authorities', AuthorityViewSet)
router.register(r'sustainability_trackings', SustainabilityTrackingViewSet)
router.register(r'support_tickets', SupportTicketViewSet)
router.register(r'vehicles', VehicleViewSet)
router.register(r'trips', TripViewSet)
router.register(r'maintenance-logs', MaintenanceLogViewSet)
router.register(r'appointments', AppointmentViewSet)
router.register(r'gps-data', GPSDataViewSet)
router.register(r'fuel-transactions', FuelTransactionViewSet)
router.register(r'user-profiles', UserProfileViewSet)
