import os
import tempfile
import unittest

import shutil
from django import test
from django.conf import settings
from devilry.utils import datetimeutils

from model_bakery import baker

from devilry.devilry_comment.models import CommentFile
from devilry.devilry_group.models import FeedbackSet, GroupComment
from devilry.devilry_import_v2database.modelimporters.delivery_feedback_importers \
    import StaticFeedbackImporter, DeliveryImporter
from .importer_testcase_mixin import ImporterTestCaseMixin


@unittest.skip('Not relevant anymore, keep for history.')
class TestStaticFeedbackImporterImporter(ImporterTestCaseMixin, test.TestCase):
    def setUp(self):
        self.v2_media_root_temp_dir = tempfile.mkdtemp()

    def tearDown(self):
        super(TestStaticFeedbackImporterImporter, self).tearDown()
        shutil.rmtree(self.v2_media_root_temp_dir)

    def _create_staticfeedback_dict(self, feedback_set, file_info_dict=None, examiner_user_id=None):
        return {
            'pk': 1,
            'model': 'core.staticfeedback',
            'fields': {
                'is_passing_grade': True,
                'grade': '2/4',
                'saved_by': examiner_user_id,
                'delivery': 1,
                'points': 2,
                'files': file_info_dict or {},
                'deadline_id': feedback_set.id,
                'save_timestamp': '2017-05-15T11:04:46.817',
                'rendered_view': '<p>Quo tempore facilis eos suscipit eum doloremque libero'
                                  ' veniam nisi?</p>\n<p>Magnam mollitia alias consequatur nisi'
                                  ' nam error dolor laboriosam aperiam? Nihil eligendi voluptatem,'
                                  ' eveniet iure officiis amet laborum debitis nisi in, '
                                  'molestias similique vero quos beatae obcaecati neque laudantium '
                                  'suscipit rerum repudiandae, facilis doloribus autem molestias '
                                  'asperiores perferendis est delectus alias porro laboriosam culpa, '
                                  'iusto ut aliquid et? Id iusto dolor consequatur necessitatibus explicabo '
                                  'repellendus, suscipit nisi non.</p>\n<p>Quas natus id nulla pariatur'
                                  ' similique ducimus mollitia ea tenetur veniam fugiat, rerum temporibus '
                                  'tempore eaque nemo at, nihil dolores ad ducimus delectus quasi nesciunt '
                                  'illo, aspernatur ullam officia aperiam officiis harum repellat pariatur '
                                  'quaerat deserunt sint. Debitis nam deserunt autem voluptas? Debitis libero'
                                  ' beatae deserunt et ullam expedita aliquid inventore autem nam veniam, '
                                  'dolore rem ea voluptatibus placeat explicabo.</p>'
            }
        }

    def test_importer(self):
        test_examiner_user = baker.make(settings.AUTH_USER_MODEL)
        test_group = baker.make('core.AssignmentGroup')
        baker.make('core.Examiner',
                   assignmentgroup=test_group,
                   relatedexaminer__user=test_examiner_user,
                   relatedexaminer__period=test_group.parentnode.parentnode)
        test_feedbackset = baker.make('devilry_group.FeedbackSet', group=test_group)
        self.create_v2dump(
            model_name='core.staticfeedback',
            data=self._create_staticfeedback_dict(
                feedback_set=test_feedbackset,
                examiner_user_id=test_examiner_user.id)
        )
        StaticFeedbackImporter(input_root=self.temp_root_dir).import_models()
        self.assertEqual(FeedbackSet.objects.count(), 1)
        self.assertEqual(GroupComment.objects.count(), 1)

    def test_importer_feedback_set(self):