# Path: server/daprojects_api/serializers.py
# class ProjectSerializer(serializers.HyperlinkedModelSerializer):
#     first_level_modules = serializers.HyperlinkedRelatedField(
#         view_name='module-detail',
#         read_only=True,
#         many=True,
#     )
#     class Meta:
#         model = Project
#         fields = ('url', 'name', 'slug', 'description', 'first_level_modules')
# 
# class ModuleSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Module
#         fields = ('url', 'project', 'parent', 'children', 'name', 'slug', 'path', 'description', 'directories', 'issues')
# 
# class IssueKindSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = IssueKind
#         fields = ('url', 'name')
# 
# class IssueSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Issue
#         fields = ('url', 'module', 'file_name', 'file_line', 'description', 'kind', 'size')
# 
# class DirectorySerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Directory
#         fields = ('url', 'project', 'parent', 'children', 'slug', 'path', 'modules', 'is_leaf_node')
# 
# class DirectoryTreeSerializer(serializers.Serializer):
#     '''For project directory initialization'''
#     name = serializers.CharField(max_length=255)
#     size = serializers.IntegerField()
# 
# class SyncModuleSerializer(serializers.Serializer):
#     '''For issues synchronization'''
#     module = serializers.HyperlinkedRelatedField(
#         view_name='module-detail',
#         queryset=Module.objects.all(),
#     )
#     issues = SyncIssueSerializer(many=True)
#     class Meta:
#         fields = ('module', 'issues', 'submodules')


from rest_framework import viewsets, filters, status
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from daprojects_core.models import Project, Module, IssueKind, Issue, Directory
from daprojects_core.services import init_project, sync_issues
from .serializers import (
    ProjectSerializer, ModuleSerializer, IssueKindSerializer, IssueSerializer, DirectorySerializer,
    DirectoryTreeSerializer, SyncModuleSerializer
)




class ProjectViewSet(viewsets.ModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('slug',)

    @detail_route(methods=['post'])
    def initialize(self, request, pk=None):
        project = self.get_object()
        if project.modules.exists() or project.directories.exists():
            return Response('The project must be empty', status=status.HTTP_400_BAD_REQUEST)
        dir_tree_serializer = DirectoryTreeSerializer(data=request.data, many=True)
        if dir_tree_serializer.is_valid():
            init_project(project, dir_tree_serializer.validated_data)
            return Response({'status': 'Project initialized'})
        else:

# Path: server/daprojects_api/serializers.py
# class ProjectSerializer(serializers.HyperlinkedModelSerializer):
#     first_level_modules = serializers.HyperlinkedRelatedField(
#         view_name='module-detail',
#         read_only=True,
#         many=True,
#     )
#     class Meta:
#         model = Project
#         fields = ('url', 'name', 'slug', 'description', 'first_level_modules')
# 
# class ModuleSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Module
#         fields = ('url', 'project', 'parent', 'children', 'name', 'slug', 'path', 'description', 'directories', 'issues')
# 
# class IssueKindSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = IssueKind
#         fields = ('url', 'name')
# 
# class IssueSerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Issue
#         fields = ('url', 'module', 'file_name', 'file_line', 'description', 'kind', 'size')
# 
# class DirectorySerializer(serializers.HyperlinkedModelSerializer):
#     class Meta:
#         model = Directory
#         fields = ('url', 'project', 'parent', 'children', 'slug', 'path', 'modules', 'is_leaf_node')
# 
# class DirectoryTreeSerializer(serializers.Serializer):
#     '''For project directory initialization'''
#     name = serializers.CharField(max_length=255)
#     size = serializers.IntegerField()
# 
# class SyncModuleSerializer(serializers.Serializer):
#     '''For issues synchronization'''
#     module = serializers.HyperlinkedRelatedField(
#         view_name='module-detail',
#         queryset=Module.objects.all(),
#     )
#     issues = SyncIssueSerializer(many=True)
#     class Meta:
#         fields = ('module', 'issues', 'submodules')


# Path: server/daprojects_api/views.py
from rest_framework import viewsets, filters, status
from rest_framework.decorators import detail_route
from rest_framework.response import Response
from daprojects_core.models import Project, Module, IssueKind, Issue, Directory
from daprojects_core.services import init_project, sync_issues
from .serializers import (
    ProjectSerializer, ModuleSerializer, IssueKindSerializer, IssueSerializer, DirectorySerializer,
    DirectoryTreeSerializer, SyncModuleSerializer
)





class ProjectViewSet(viewsets.ModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = ('slug',)

    @detail_route(methods=['post'])
    def initialize(self, request, pk=None):
        project = self.get_object()
        if project.modules.exists() or project.directories.exists():
            return Response('The project must be empty', status=status.HTTP_400_BAD_REQUEST)
        dir_tree_serializer = DirectoryTreeSerializer(data=request.data, many=True)
        if dir_tree_serializer.is_valid():
            init_project(project, dir_tree_serializer.validated_data)
            return Response({'status': 'Project initialized'})
        else:

            return Response(dir_tree_serializer.errors, status=status.HTTP_400_BAD_REQUEST)