from django.test import TestCase

from bayer.models import PESCompound, PESStructure
from epdb.logic import UserManager
from epdb.models import CompoundStructure, Compound


class PESTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.user = UserManager.create_user(
            "test-user",
            "test-user@test.com",
            "TestPass123",
            set_setting=False,
            add_to_group=False,
            is_active=True,
        )

        cls.pes_dummy = {
            "representativeStructures": [
                {
                    "ctab": "\n     RDKit          2D\n\n 14 15  0  0  0  0  0  0  0  0999 V2000\n    2.7760    0.0000    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    1.2760    0.0000    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n    0.3943    1.2135    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n    0.7062    2.6807    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -0.4086    3.6844    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n   -0.0967    5.1517    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -1.8351    3.2209    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -2.1470    1.7537    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n   -3.5736    1.2902    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -1.0323    0.7500    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -1.0323   -0.7500    0.0000 N   0  0  0  0  0  0  0  0  0  0  0  0\n    0.3943   -1.2135    0.0000 C   0  0  0  0  0  0  0  0  0  0  0  0\n   -2.9499    4.2246    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0\n    2.1328    3.1443    0.0000 O   0  0  0  0  0  0  0  0  0  0  0  0\n  1  2  1  0\n  2  3  1  0\n  3  4  1  0\n  4  5  1  0\n  5  6  1  0\n  5  7  1  0\n  7  8  1  0\n  8  9  1  0\n  8 10  1  0\n 10 11  1  0\n 11 12  2  0\n  7 13  2  0\n  4 14  2  0\n 12  2  1  0\n 10  3  2  0\nM  END\n"
                }
            ],
            "representations": [
                {
                    "renderingOptions": {
                        "molecularFormulaRenderingOption": {
                            "displayOnRepresentation": False,
                            "labelCoords": {
                                "x": 0,
                                "y": 0
                            }
                        },
                        "modificationRenderingOptions": [
                            {
                                "localModificationId": 1,
                                "displayNominalMass": False,
                                "displaySumFormula": False
                            }
                        ],
                        "contouringType": "convexHull",
                        "colored": True,
                        "abbreviations": False,
                        "structureSize": 0,
                        "showDirection": None,
                        "showStereoFlags": False
                    },
                    "type": "black-and-white",
                    "mimeType": "image/png",
                    "base64": "data:image/png;base64,[encoded data, 8088 characters]"
                },
                {
                    "renderingOptions": {
                        "molecularFormulaRenderingOption": {
                            "displayOnRepresentation": False,
                            "labelCoords": {
                                "x": 0,
                                "y": 0
                            }
                        },
                        "modificationRenderingOptions": [
                            {
                                "localModificationId": 1,
                                "displayNominalMass": False,
                                "displaySumFormula": False
                            }
                        ],
                        "contouringType": "convexHull",
                        "colored": True,
                        "abbreviations": False,
                        "structureSize": 0,
                        "showDirection": None,
                        "showStereoFlags": False
                    },
                    "type": "color",
                    "mimeType": "image/png",
                    "base64": "data:image/png;base64,[encoded data, 8088 characters]"
                }
            ],
            "lifecycleStatus": "Published",
            "corporateId": "PES-000126",
            'pes_url': 'https://pesregapp-test.cropkey-np.ag/entities/PES-000126',
            "classificationLevel": "Restricted",
            "dataPools": None,
        }

    def test_smoke(self):
        pes_compound = PESCompound.create(
            self.user.default_package,
            self.pes_dummy,
            'Test PES',
            'Test Description'
        )

        self.assertEqual(PESCompound.objects.count(), 1)

        obj = PESCompound.objects.first()

        self.assertEqual(obj.name, 'Test PES')
        self.assertEqual(obj.description, 'Test Description')
        self.assertTrue(isinstance(obj.default_structure, PESStructure))
        self.assertEqual(obj.default_structure.pes_link, 'https://pesregapp-test.cropkey-np.ag/entities/PES-000126')
        self.assertEqual(obj.default_structure.smiles, "Cn1c2c(=O)n(C)c(=O)n(C)c2nc1")
        self.assertTrue(isinstance(obj.normalized_structure, PESStructure))
        self.assertEqual(obj.normalized_structure.pes_link, 'https://pesregapp-test.cropkey-np.ag/entities/PES-000126')
        self.assertEqual(obj.normalized_structure.smiles, "CN1C(=O)C2=C(N=CN2C)N(C)C1=O")

        self.assertEqual(CompoundStructure.objects.count(), 2)
        self.assertEqual(PESStructure.objects.count(), 2)


    def test_pes_dedup(self):
        _ = PESCompound.create(
            self.user.default_package,
            self.pes_dummy,
            'Test PES',
            'Test Description'
        )

        self.assertEqual(PESCompound.objects.count(), 1)

        _ = PESCompound.create(
            self.user.default_package,
            self.pes_dummy,
            'Test PES 2',
            'Test Description 2'
        )

        # Assert deduplication works we only have one object
        self.assertEqual(PESCompound.objects.count(), 1)

        obj = PESCompound.objects.first()

        # Assert name and description remain
        self.assertEqual(obj.name, 'Test PES')
        self.assertEqual(obj.description, 'Test Description')


    def test_add_pes_and_compound_with_same_representative(self):
        pes = PESCompound.create(
            self.user.default_package,
            self.pes_dummy,
            'Test PES',
            'Test Description'
        )

        regular = Compound.create(
            self.user.default_package,
            "CN1C(=O)C2=C(N=CN2C)N(C)C1=O",  # Already normalized
            'Test PES',
            'Test Description'
        )

        self.assertNotEqual(pes, regular)
        self.assertTrue(isinstance(pes, PESCompound))
        self.assertTrue(isinstance(regular, Compound))
        self.assertEqual(Compound.objects.count(), 2)
        self.assertEqual(PESCompound.objects.count(), 1)

        self.assertEqual(CompoundStructure.objects.count(), 3)




    # def test_api_add_pes(self):
    #
    #
    #     def test_my_endpoint():
    #         client = TestClient(router, auth=MockMSAuth())
    #         response = client.get("/my-endpoint", headers={"Authorization": "Bearer mock-token"})
    #         assert response.status_code == 200
