from api.test_utils import DefaultUserTestCase, create_client
from assignments.factories import AssignmentFactory, StudentSubmissionFactory
from books.factories import ModuleFactory, TopicFactory
from core.logger import get_logger

logger = get_logger(__name__)


class MyAssignmentsTest(DefaultUserTestCase):
    def setUp(self):
        super(MyAssignmentsTest, self).setUp()
        topic = TopicFactory()
        module = ModuleFactory(parent=topic)
        self.assignment = AssignmentFactory(owner=self.teacher, module=module)

        self.submission1 = StudentSubmissionFactory(
            student=self.student1, assignment=self.assignment
        )
        self.submission2 = StudentSubmissionFactory(
            student=self.student2, assignment=self.assignment
        )

        self.client = create_client(self.student1)

    def query_my_assignments(self):
        query = """
  fragment HighlightParts on HighlightNode {
    id
    contentIndex
    paragraphIndex
    selectionLength
    contentUuid
    startPosition
    color
    note {
      text
    }
    text
    page {
      # only one of them should be necessary, but the client somehow doesn't like just the Node inline fragment
      __typename
      ... on ContentBlockNode {
        id
        path
      }
      ... on InstrumentNode {
        id
        slug
      }
      ... on ModuleNode {
        id
        slug
        path
      }
      ... on ChapterNode {
        id
        path
        slug
      }
    }
  }
    query MyActivitiesQuery {
      myActivities {
        instruments {
          id
          slug
          title
          path
          highlights {
            ...HighlightParts
          }
          bookmarks {
            ... on InstrumentBookmarkNode {
              path
            }
          }
        }
        topics {
          id
          title
          modules {
            id
            slug
            title
            metaTitle
            myHighlights {
              ...HighlightParts
            }
            myBookmarks {
              ... on ChapterBookmarkNode {
                chapter {
                  path
                }
                path
                note {
                  id
                  text
                }
              }
              ... on ContentBlockBookmarkNode {
                id
                uuid
                path
                contentBlock {
                  id
                  path
                }
                note {
                  id
                  text
                }
              }
              ... on ModuleBookmarkNode {
                path
                note {
                  id
                  text
                }
              }
            }
            mySubmissions {
              id
              text
              assignment {
                id
                title
                path
                module {
                  slug
                }
              }
            }
            myAnswers {
              id
              survey {
                path
                id
                title
              }
            }
          }
        }
      }
    }
        """

        result = self.client.execute(query)

        self.assertIsNone(result.get("errors"))
        return result

    @staticmethod
    def get_submissions(result):
        return (
            result.get("data")
            .get("myActivities")
            .get("topics")[0]
            .get("modules")[0]
            .get("mySubmissions")
        )

    def test_my_assignment_query(self):
        result = self.query_my_assignments()
        submissions = self.get_submissions(result)
        logger.debug(submissions)
        self.assertEqual(len(submissions), 1)
        self.assertEqual(
            submissions[0].get("text"),
            self.submission1.text,
        )
