"""Test utils module"""

import json
from pathlib import Path

import pytest
from deepdiff import DeepDiff

from therapy.database.database import AbstractDatabase
from therapy.schemas import RecordType, SourceName
from therapy.utils import get_term_mappings


@pytest.fixture(scope="module")
def test_db(null_database_class, test_data: Path):
    with (test_data / "fixtures" / "merged_fixtures.json").open() as f:
        merged_fixtures = list(json.load(f).values())
    return null_database_class(
        get_all_records={
            RecordType.IDENTITY: [
                {
                    "associated_with": [
                        "drugcentral:1892",
                        "inchikey:IXOXBSCIXZEQEQ-UHTZMRCNSA-N",
                        "pubchem.compound:3011155",
                        "pubchem.substance:178103668",
                    ],
                    "merge_ref": "rxcui:274771",
                    "xrefs": [
                        "chembl:CHEMBL1201112",
                        "chemidplus:121032-29-9",
                        "drugbank:DB01280",
                    ],
                    "aliases": [
                        "(2R,3S,4S,5R)-2-(hydroxymethyl)-5-(2-imino-6-methoxy-3,9-dihydro-2H-purin-9-yl)oxolane-3,4-diol",
                        "506U78",
                        "Arranon",
                        "Atriance",
                    ],
                    "label": "nelarabine",
                    "src_name": "GuideToPHARMACOLOGY",
                    "concept_id": "iuphar.ligand:7090",
                    "approval_ratings": ["gtopdb_approved"],
                    "label_and_type": "iuphar.ligand:7090##identity",
                    "item_type": "identity",
                },
                {
                    "merge_ref": "rxcui:224905",
                    "xrefs": ["rxcui:224905"],
                    "trade_names": [
                        "Biceltis",
                        "CANMab",
                        "Herceptin",
                        "Herclon",
                        "Hertraz",
                    ],
                    "label": "Trastuzumab",
                    "approval_year": ["1998"],
                    "src_name": "HemOnc",
                    "concept_id": "hemonc:512",
                    "approval_ratings": ["hemonc_approved"],
                    "label_and_type": "hemonc:512##identity",
                    "has_indication": [
                        '["hemonc:601", "Gastric cancer", "ncit:C9331", {"regulatory_body": "FDA"}]',
                        '["hemonc:572", "Breast cancer", "ncit:C9335", {"regulatory_body": "FDA"}]',
                    ],
                    "item_type": "identity",
                },
                {
                    "associated_with": ["umls:C0728747", "unii:P188ANX8CK"],
                    "merge_ref": "rxcui:224905",
                    "xrefs": ["chemidplus:180288-69-1"],
                    "label": "Trastuzumab",
                    "src_name": "NCIt",
                    "concept_id": "ncit:C1647",
                    "label_and_type": "ncit:c1647##identity",
                    "item_type": "identity",
                },
                {
                    "associated_with": [
                        "CHEBI:63612",
                        "umls:C0907349",
                        "unii:60158CV180",
                    ],
                    "merge_ref": "rxcui:274771",
                    "xrefs": ["chemidplus:121032-29-9"],
                    "aliases": [
                        "2-Amino, 6-Methoxypurine Arabinoside",
                        "2-Amino-6-methoxypurine arabinoside",
                        "2-Amino-9-beta-D-arabinofuranosyl-6-methoxy-9H-purine",
                        "506U78",
                        "Arranon",
                        "Compound 506U78",
                        "GW506U78",
                        "NELARABINE",
                        "nelarabine",
                    ],
                    "label": "Nelarabine",
                    "src_name": "NCIt",
                    "concept_id": "ncit:C1704",
                    "label_and_type": "ncit:c1704##identity",
                    "item_type": "identity",
                },
            ],
            RecordType.MERGER: merged_fixtures,
        }
    )


@pytest.fixture
def expected_ncit_mappings():
    return [
        {
            "xrefs": ["umls:C0728747", "unii:P188ANX8CK", "chemidplus:180288-69-1"],
            "label": "Trastuzumab",
            "concept_id": "ncit:C1647",
            "trade_names": [],
            "aliases": [],
        },
        {
            "xrefs": [
                "CHEBI:63612",
                "umls:C0907349",
                "unii:60158CV180",
                "chemidplus:121032-29-9",
            ],
            "aliases": [
                "2-Amino, 6-Methoxypurine Arabinoside",
                "2-Amino-6-methoxypurine arabinoside",
                "2-Amino-9-beta-D-arabinofuranosyl-6-methoxy-9H-purine",
                "506U78",
                "Arranon",
                "Compound 506U78",
                "GW506U78",
                "NELARABINE",
                "nelarabine",
            ],
            "label": "Nelarabine",
            "concept_id": "ncit:C1704",
            "trade_names": [],
        },
    ]


def test_get_term_mappings_ncit(test_db: AbstractDatabase, expected_ncit_mappings):
    results = list(get_term_mappings(test_db, SourceName.NCIT))

    diff = DeepDiff(expected_ncit_mappings, results, ignore_order=True)
    assert diff == {}


def test_get_term_mappings_identity(test_db: AbstractDatabase, expected_ncit_mappings):
    results = list(get_term_mappings(test_db, scope=RecordType.IDENTITY))

    expected = [
        *expected_ncit_mappings,
        {
            "xrefs": [
                "drugcentral:1892",
                "inchikey:IXOXBSCIXZEQEQ-UHTZMRCNSA-N",
                "pubchem.compound:3011155",
                "pubchem.substance:178103668",
                "chembl:CHEMBL1201112",
                "chemidplus:121032-29-9",
                "drugbank:DB01280",
            ],
            "aliases": [
                "(2R,3S,4S,5R)-2-(hydroxymethyl)-5-(2-imino-6-methoxy-3,9-dihydro-2H-purin-9-yl)oxolane-3,4-diol",
                "506U78",
                "Arranon",
                "Atriance",
            ],
            "label": "nelarabine",
            "concept_id": "iuphar.ligand:7090",
            "trade_names": [],
        },
        {
            "xrefs": ["rxcui:224905"],
            "trade_names": [
                "Biceltis",
                "CANMab",
                "Herceptin",
                "Herclon",
                "Hertraz",
            ],
            "label": "Trastuzumab",
            "concept_id": "hemonc:512",
            "aliases": [],
        },
    ]
    diff = DeepDiff(expected, results, ignore_order=True)
    assert diff == {}


def test_get_term_mappings_merger(test_db: AbstractDatabase):
    results = list(get_term_mappings(test_db, scope=RecordType.MERGER))

    expected = [
        {
            "concept_id": "rxcui:8134",
            "xrefs": [
                "ncit:C739",
                "drugbank:DB01174",
                "chemidplus:50-06-6",
                "wikidata:Q407241",
                "chembl:CHEMBL40",
                "iuphar.ligand:2804",
                "pubchem.substance:135650817",
                "usp:m63400",
                "pubchem.compound:4763",
                "unii:YQE403BP4D",
                "vandf:4017422",
                "drugcentral:2134",
                "mmsl:d00340",
                "umls:C0031412",
                "mmsl:2390",
                "inchikey:DDBREPKUVSBGFI-UHFFFAOYSA-N",
                "atc:N03AA02",
                "mmsl:5272",
                "CHEBI:8069",
            ],
            "aliases": [
                "PHENobarbital",
                "Phenobarbitone",
                "NSC-128143",
                "5-Ethyl-5-phenyl-pyrimidine-2,4,6-trione",
                "5-Phenyl-5-ethylbarbituric acid",
                "5-ethyl-5-phenyl-1,3-diazinane-2,4,6-trione",
                "Phenobarbituric Acid",
                "Phenylethylbarbituric Acid",
                "PHENOBARBITAL",
                "phenobarbitone",
                "Phenylethylbarbiturs\u00e4ure",
                "5-Ethyl-5-phenylbarbituric acid",
                "NSC-128143-",
                "phenobarb",
                "Luminal",
                "5-ethyl-5-phenyl-2,4,6(1H,3H,5H)-pyrimidinetrione",
                "PHENYLETHYLMALONYLUREA",
                "Phenylethylbarbiturate",
                "Luminal\u00ae",
                "Phenemal",
                "fenobarbital",
                "Fenobarbital",
                "Phenylethylbarbitursaeure",
                "5-Ethyl-5-phenyl-2,4,6(1H,3H,5H)-pyrimidinetrione",
                "phenylethylbarbiturate",
                "Phenobarbital civ",
                "Phenylaethylbarbitursaeure",
                "Phenobarbitol",
                "NSC-9848",
                "Phenyl\u00e4thylbarbiturs\u00e4ure",
                "phenobarbital",
                "5-ethyl-5-phenylpyrimidine-2,4,6(1H,3H,5H)-trione",
                "APRD00184",
                "Phenylethylmalonylurea",
                "PHENO",
                "phenobarbital sodium",
                "Phenobarbital",
            ],
            "trade_names": [
                "Eskabarb",
                "Solfoton",
                "Phenobarbitone",
                "Noptil",
                "Luminal",
                "Talpheno",
            ],
            "label": "phenobarbital",
        },
        {
            "concept_id": "rxcui:2555",
            "aliases": [
                "CISplatin",
                "APRD00359",
                "1,2-Diaminocyclohexaneplatinum II citrate",
                "CIS-DDP",
                "(sp-4-2)-diamminedichloroplatinum",
                "CIS-DIAMMINEDICHLOROPLATINUM",
                "CDDP",
                "INT-230-6 COMPONENT CISPLATIN",
                "CIS-DIAMMINEDICHLOROPLATINUM II",
                "PLATINUM, DIAMMINEDICHLORO-, (SP-4-2)-",
                "DDP",
                "Fauldiscipla",
                "cis-Diaminedichloroplatinum",
                "Platinol-AQ",
                "cis-diamminedichloroplatinum III",
                "Peyrone's salt",
                "NSC119875",
                "cis-diamminedichloroplatinum(II)",
                "(SP-4-2)-DIAMMINEDICHLOROPLATINUM",
                "DACP",
                "Cis-diamminedichloroplatinum",
                "Platinol",
                "Liplacis",
                "cis-platinum",
                "Cis-DDP",
                "Cisplatinum",
                "Cis-platin",
                "Cis-platinum ii",
                "NSC-119875",
                "cisplatinum",
                "INT230-6 COMPONENT CISPLATIN",
                "cisplatino",
                "Cis-diamminedichloroplatinum ii",
                "NSC 119875",
                "CISPLATIN",
                "Cisplatin",
                "Mitometh",
            ],
            "xrefs": [
                "ndc:0143-9505",
                "usp:m17910",
                "inchikey:MOTIYCLHZZLHHQ-UHFFFAOYSA-N",
                "spl:c3ddc4a5-9f1b-a8ee-e053-2a95a90a2265",
                "pubchem.compound:441203",
                "ndc:25021-253",
                "ndc:44567-510",
                "spl:a66eda32-1164-439a-ac8e-73138365ec06",
                "mmsl:4456",
                "ndc:44567-511",
                "umls:C0008838",
                "mmsl:d00195",
                "ndc:70860-206",
                "mmsl:31747",
                "unii:H8MTN7XVC2",
                "ndc:44567-530",
                "atc:L01XA01",
                "spl:01c7a680-ee0d-42da-85e8-8d56c6fe7006",
                "spl:adf5773e-9095-4cb4-a90f-72cbf82f4493",
                "spl:dd45d777-d4c1-40ee-b4f0-c9e001a15a8c",
                "spl:89007399-3c34-40d2-8068-a0b8e09cbef8",
                "pubchem.compound:5702198",
                "vandf:4018139",
                "ndc:68083-162",
                "ndc:63323-103",
                "ndc:68001-283",
                "unii:Q20Q21Q62J",
                "spl:64bcce1a-6e31-4e73-8da5-11aa9e890da2",
                "ndc:68083-163",
                "spl:2c569ef0-588f-4828-8b2d-03a2120c9b4c",
                "spl:c43de769-d6d8-3bb9-e053-2995a90a5aa2",
                "ndc:72266-252",
                "ndc:0703-5747",
                "ndc:44567-509",
                "ndc:16729-288",
                "ndc:0703-5748",
                "ndc:72266-253",
                "spl:0219ee77-eb38-b81f-e063-6394a90a5ca4",
                "CHEBI:27899",
                "ndc:0143-9504",
                "pubchem.substance:178102005",
                "spl:9b008181-ab66-db2f-e053-2995a90aad57",
                "inchikey:LXZZYRPGZAFOLE-UHFFFAOYSA-L",
                "ncit:C376",
                "hemonc:105",
                "drugbank:DB00515",
                "drugbank:DB12117",
                "drugsatfda.anda:074656",
                "drugsatfda.anda:074735",
                "drugsatfda.anda:075036",
                "drugsatfda.anda:206774",
                "drugsatfda.anda:207323",
                "drugsatfda.nda:018057",
                "iuphar.ligand:5343",
                "chembl:CHEMBL11359",
                "chemidplus:15663-27-1",
                "wikidata:Q412415",
            ],
            "label": "cisplatin",
            "trade_names": [
                "Platinol",
                "Cisplatin",
                "Platinol-aq",
                "PLATINOL-AQ",
                "PLATINOL",
            ],
        },
        {
            "concept_id": "rxcui:9991",
            "xrefs": [
                "ncit:C839",
                "drugbank:DB06145",
                "chemidplus:8025-81-8",
                "wikidata:Q422265",
                "pubchem.compound:5356392",
                "umls:C0037962",
                "unii:71ODY0V87H",
                "inchikey:ACTOXUHEUCPTEW-CEUOBAOPSA-N",
                "atc:J01FA02",
                "unii:033ECH6IFG",
            ],
            "label": "spiramycin",
            "trade_names": [],
            "aliases": [
                "RP 5337",
                "Spiramycin",
                "SPIRAMYCIN",
                "Foromacidine A",
                "Provamycin",
                "Foromacidin A",
                "Antibiotic 799",
                "spiramycin I",
                "Rovamycin",
                "Spiramycin A",
                "Spiramycin 1",
                "(4R,5S,6R,7R,9R,10R,11E,13E,16R)-10-{[(2R,5S,6R)-5-(dimethylamino)-6-methyltetrahydro-2H-pyran-2-yl]oxy}-9,16-dimethyl-5-methoxy-2-oxo-7-(2-oxoethyl)oxacyclohexadeca-11,13-dien-6-yl 3,6-dideoxy-4-O-(2,6-dideoxy-3-C-methyl-alpha-L-ribo-hexopyranosyl)-3-(dimethylamino)-alpha-D-glucopyranoside",
                "Spiramycin I",
                "Rovamycine",
                "Demycarosylturimycin H",
            ],
        },
        {
            "aliases": [
                "Gammaphos",
                "Apaetp",
                "S-(N-(3-Aminopropyl)-2-aminoethyl)thiophosphoric Acid Trihydrate",
                "S-2-(3-Aminopropylamino)ethylphosphorothioic Acid Trihydrate",
                "S-(2-((3-Aminopropyl)amino)ethyl) Dihydrogen Phosphorothioate",
                "Amifostine disulfide",
                "Ethiofos",
                "AMIFOSTINE",
                "amifostine",
                "NSC-758236",
                "Amifostine Anhydrous",
                "Amifostine anhydrous",
                "Ethiofos Anhydrous",
                "WR 2721",
                "YM-08310",
                "Aminopropylaminoethyl thiophosphate",
                "Amifostine trihydrate",
                "Amifostina",
                "ethiofos anhydrous",
                "APRD00021",
                "WR-1065",
                "Amifostine Trihydrate",
                "SAPEP",
                "WR2721",
                "NSC-296961",
                "Ethyol",
                "Amifostinum",
                "2-[(3-Aminopropyl)amino]ethanethiol Dihydrogen Phosphate Ester Trihydrate",
                "Amifostine Ethiofos",
                "WR-2721",
                "Aminopropylaminoethyl Thiophosphate",
                "APAETP",
                "Ethiofos anhydrous",
                "Amifostine",
                "Cytofos",
                "WR-2721 TRIHYDRATE",
                "amifostine anhydrous",
                "Aminopropylaminoethylthiophosphoric Acid Trihydrate",
                "AMIFOSTINE ANHYDROUS",
            ],
            "label": "amifostine anhydrous",
            "concept_id": "rxcui:1545987",
            "xrefs": [
                "umls:C0015020",
                "CHEBI:2636",
                "mmsl:d03868",
                "vandf:4020970",
                "inchikey:JKOQGQFVAUAYPM-UHFFFAOYSA-N",
                "umls:C3537278",
                "mmsl:133029",
                "usp:m2600",
                "mmsl:4164",
                "ndc:76310-017",
                "atc:V03AF05",
                "pubchem.compound:2141",
                "unii:ILA426L95O",
                "unii:M487QF2F4V",
                "spl:cc8db98c-87bc-461f-8d88-8fbf482c6a3c",
                "rxcui:4126",
                "ncit:C488",
                "ncit:C66724",
                "drugbank:DB01143",
                "drugsatfda.nda:020221",
                "chembl:CHEMBL1006",
                "chemidplus:112901-68-5",
                "chemidplus:20537-88-6",
                "wikidata:Q251698",
            ],
            "trade_names": ["Amifostine", "Ethyol", "ETHYOL"],
        },
    ]

    diff = DeepDiff(expected, results, ignore_order=True)
    assert diff == {}
