from datetime import timedelta, date

from graphql_relay import to_global_id

from assignments.factories import AssignmentFactory, StudentSubmissionFactory
from assignments.tests.queries.mutations import UPDATE_ASSIGNMENT_MUTATION, UPDATE_SUBMISSION_FEEDBACK_MUTATION
from core.tests.base_test import SkillboxTestCase
from users.models import User


class AssignmentReadOnlyTestCase(SkillboxTestCase):
    def setUp(self) -> None:
        self.createDefault()
        yesterday = date.today() - timedelta(days=1)

        self.student1.license_expiry_date = yesterday
        self.student1.save()
        self.teacher.license_expiry_date = yesterday
        self.teacher.save()
        self.assignment = AssignmentFactory()
        self.assignment_id = to_global_id('AssignmentNode', self.assignment.id)

    def test_edit_assignment_fails(self):
        variables = {
            "input": {
                "assignment": {
                    "answer": "bla",
                    "document": "",
                    "final": False,
                    "id": self.assignment_id
                }
            }
        }
        result = self.get_client(self.student1).execute(UPDATE_ASSIGNMENT_MUTATION, variables=variables)
        self.assertIsNotNone(result.get('errors'))

    def test_share_assignment_fails(self):
        variables = {
            "input": {
                "assignment": {
                    "answer": "bla",
                    "document": "",
                    "final": True,
                    "id": self.assignment_id
                }
            }
        }
        result = self.get_client(self.student1).execute(UPDATE_ASSIGNMENT_MUTATION, variables=variables)
        self.assertIsNotNone(result.get('errors'))

    def test_edit_feedback_fails(self):
        student_submission = StudentSubmissionFactory(assignment=self.assignment, student=self.student1,
                                                           final=True)
        student_submission_id = to_global_id('StudentSubmissionNode', student_submission.id)
        result = self.get_client(self.teacher).execute(UPDATE_SUBMISSION_FEEDBACK_MUTATION, variables={
            'input': {
                "submissionFeedback": {
                    "studentSubmission": student_submission_id,
                    "text": "Feedback",
                    "final": False
                }
            }
        })

        self.assertIsNotNone(result.get('errors'))


    def test_share_feedback_fails(self):
        student_submission = StudentSubmissionFactory(assignment=self.assignment, student=self.student1,
                                                           final=True)
        student_submission_id = to_global_id('StudentSubmissionNode', student_submission.id)
        result = self.get_client(self.teacher).execute(UPDATE_SUBMISSION_FEEDBACK_MUTATION, variables={
            'input': {
                "submissionFeedback": {
                    "studentSubmission": student_submission_id,
                    "text": "Feedback",
                    "final": True
                }
            }
        })

        self.assertIsNotNone(result.get('errors'))
