from django.db.models import Q
from rest_framework.exceptions import NotFound, PermissionDenied, ValidationError
from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet

from apps.accounts.models import Organization, Role
from apps.accounts.permissions import can_manage_organization
from apps.knowledge.models import RoleRagDocument, TrainingFile
from apps.knowledge.serializers import RoleRagDocumentSerializer, TrainingFileSerializer
from apps.knowledge.tasks import ingest_training_file_task, update_agent_prompts_from_file_task

class TrainingFileViewSet(ModelViewSet):
    queryset = TrainingFile.objects.all()
    serializer_class = TrainingFileSerializer
    permission_classes = [IsAuthenticated]
    parser_classes = [MultiPartParser, FormParser]
    lookup_field = 'uuid'
    
    def get_queryset(self):
        user = self.request.user
        queryset = TrainingFile.objects.filter(
            Q(organization__owner=user) |
            Q(organization__members=user)
        ).distinct()

        organization_uuid = self.request.query_params.get('organization_uuid')
        if organization_uuid in (None, ''):
            organization_uuid = self.request.data.get('organization_uuid')
        if organization_uuid:
            queryset = queryset.filter(organization__uuid=organization_uuid)

        role_uuid = self.request.query_params.get('role_uuid')
        if role_uuid in (None, ''):
            role_uuid = self.request.data.get('role_uuid')
        if role_uuid:
            queryset = queryset.filter(Q(role__uuid=role_uuid) | Q(role__isnull=True))

        return queryset

    def perform_create(self, serializer):
        role_uuid = self.request.data.get('role_uuid')
        organization_uuid = self.request.data.get('organization_uuid')

        role = None
        organization = None

        if role_uuid:
            try:
                role = Role.objects.select_related('organization').get(uuid=role_uuid)
            except Role.DoesNotExist:
                raise NotFound('Role not found')

            organization = role.organization

            if organization_uuid and str(organization.uuid) != str(organization_uuid):
                raise ValidationError({'organization_uuid': 'organization_uuid does not match role organization.'})
        else:
            if not organization_uuid:
                raise ValidationError({'organization_uuid': 'organization_uuid is required when role_uuid is not provided.'})

            try:
                organization = Organization.objects.get(uuid=organization_uuid)
            except Organization.DoesNotExist:
                raise NotFound('Organization not found')

        if not can_manage_organization(self.request.user, organization):
            raise PermissionDenied('Permission denied')

        uploaded_file = self.request.FILES.get('file')
        if uploaded_file is None:
            raise ValidationError({'file': 'File is required.'})

        serializer.save(
            uploaded_by=self.request.user,
            organization=organization,
            role=role,
            file_name=uploaded_file.name,
            file_size=uploaded_file.size,
            file_type=uploaded_file.content_type,
        )

    @action(detail=True, methods=['post'], url_path='retry')
    def retry(self, request, *args, **kwargs):
        instance: TrainingFile = self.get_object()

        if not can_manage_organization(request.user, instance.organization):
            raise PermissionDenied('Permission denied')

        if instance.status != 'failed':
            raise ValidationError({'status': 'Only failed files can be retried.'})

        instance.status = 'ingesting'
        instance.is_processed = False
        instance.save(update_fields=['status', 'is_processed'])
        ingest_training_file_task.delay(str(instance.uuid))

        serializer = self.get_serializer(instance)
        return Response(serializer.data)

    def destroy(self, request, *args, **kwargs):
        instance: TrainingFile = self.get_object()

        if not can_manage_organization(request.user, instance.organization):
            raise PermissionDenied('Permission denied')

        role_uuid = str(instance.role.uuid) if instance.role_id else None
        response = super().destroy(request, *args, **kwargs)
        if role_uuid:
            update_agent_prompts_from_file_task.delay(role_uuid)
        return response

class RoleRagDocumentViewSet(ReadOnlyModelViewSet):
    queryset = RoleRagDocument.objects.all()
    serializer_class = RoleRagDocumentSerializer
    permission_classes = [IsAuthenticated]
    lookup_field = 'uuid'
    
    def get_queryset(self):
        user = self.request.user
        queryset = RoleRagDocument.objects.filter(
            Q(organization__owner=user) |
            Q(organization__members=user)
        ).distinct()

        organization_uuid = self.request.query_params.get('organization_uuid')
        if organization_uuid in (None, ''):
            organization_uuid = self.request.data.get('organization_uuid')
        if organization_uuid:
            queryset = queryset.filter(organization__uuid=organization_uuid)

        role_uuid = self.request.query_params.get('role_uuid')
        if role_uuid in (None, ''):
            role_uuid = self.request.data.get('role_uuid')
        if role_uuid:
            queryset = queryset.filter(role__uuid=role_uuid)

        return queryset
