from src.superannotate import SAClient
from tests.integration.base import BaseTestCase

sa = SAClient()


class TestGetProjectMetadata(BaseTestCase):
    PROJECT_NAME = "TestGetProjectMetadata"
    PROJECT_TYPE = "Vector"
    PROJECT_DESCRIPTION = "DESCRIPTION"
    IGNORE_KEYS = {"id", "creator_id", "team_id", "createdAt", "updatedAt"}
    EXPECTED_PROJECT_METADATA_KEYS = [
        "createdAt",
        "updatedAt",
        "id",
        "team_id",
        "name",
        "type",
        "description",
        "instructions_link",
        "creator_id",
        "entropy_status",
        "sharing_status",
        "status",
        "folder_id",
        "workflow_id",
        "workflow",
        "upload_state",
        "contributors",
        "settings",
        "classes",
        "item_count",
        "completed_items_count",
        "root_folder_completed_items_count",
        "custom_fields",
    ]

    def test_get_project_metadata(self):
        # without includes
        project_metadata = sa.get_project_metadata(self.PROJECT_NAME)
        for key in self.EXPECTED_PROJECT_METADATA_KEYS:
            assert key in project_metadata
        assert project_metadata["name"] == self.PROJECT_NAME
        assert project_metadata["type"] == self.PROJECT_TYPE
        assert project_metadata["description"] == self.PROJECT_DESCRIPTION
        assert project_metadata["workflow_id"] == 1
        assert not project_metadata["workflow"]
        assert not project_metadata["contributors"]
        assert not project_metadata["completed_items_count"]
        assert not project_metadata["custom_fields"]
        assert not project_metadata["settings"]
        assert not project_metadata["classes"]

        # with includes
        project_metadata = sa.get_project_metadata(
            self.PROJECT_NAME,
            include_contributors=True,
            include_complete_item_count=True,
            include_custom_fields=True,
            include_workflow=True,
            include_settings=True,
            include_annotation_classes=True,
        )
        for key in self.EXPECTED_PROJECT_METADATA_KEYS:
            assert key in project_metadata
        assert project_metadata["name"] == self.PROJECT_NAME
        assert project_metadata["type"] == self.PROJECT_TYPE
        assert project_metadata["description"] == self.PROJECT_DESCRIPTION
        assert project_metadata["workflow_id"] == 1
        assert not project_metadata["contributors"]
        assert project_metadata["completed_items_count"] == 0
        assert project_metadata["settings"]
        assert not project_metadata["classes"]
        assert project_metadata["workflow"]
        assert project_metadata["workflow"]["id"] == 1
        assert project_metadata["workflow"]["name"] == "System workflow"
        assert project_metadata["workflow"]["createdAt"]
        assert project_metadata["workflow"]["createdAt"]
        assert project_metadata["workflow"]["type"] == "system"
        assert (
            project_metadata["workflow"]["description"]
            == "This workflow is generated by the system, and prevents annotators from completing items."
        )
        assert project_metadata["workflow"]["raw_config"]

    def test_get_project_by_id(self):
        project_metadata = sa.get_project_metadata(self.PROJECT_NAME)
        project_by_id = sa.get_project_by_id(project_metadata["id"])
        assert project_by_id["name"] == self.PROJECT_NAME
