import pytest
from devsecops_engine_tools.engine_utilities.utils.api_error import ApiError
from devsecops_engine_tools.engine_utilities.settings import DEVSECOPS_ENGINE_UTILITIES_PATH
from unittest.mock import MagicMock
from devsecops_engine_tools.engine_utilities.defect_dojo.domain.models.cmdb import Cmdb
from devsecops_engine_tools.engine_utilities.defect_dojo.domain.serializers.import_scan import ImportScanSerializer
from devsecops_engine_tools.engine_utilities.defect_dojo.infraestructure.driver_adapters.cmdb import (
    CmdbRestConsumer,
)
from devsecops_engine_tools.engine_utilities.defect_dojo.domain.request_objects.import_scan import (
    ImportScanRequest,
)
from devsecops_engine_tools.engine_utilities.defect_dojo.domain.user_case.cmdb import CmdbUserCase


def import_scan_request_instance(par_scan_type) -> ImportScanRequest:
    request = ImportScanRequest(
        product_name="test product name",
        token="123456789",
        host="https://test/test.com",
        token_defect_dojo="123456789101212",
        host_defect_dojo="http://localhost:8000",
        scan_type=par_scan_type,
        engagement_name="test engagement name",
        file=f"{DEVSECOPS_ENGINE_UTILITIES_PATH}/defect_dojo/tests/files/xray_scan.json",
        tags="evc",
    )
    return request


def get_cmdb_instance():
    mock_rest_consumer_cmdb = MagicMock(spec=CmdbRestConsumer)
    mock_rest_consumer_cmdb.get_product_info.return_value = Cmdb(
        product_type_name="type name test",
        product_name="product name test",
        tag_product="tag product test",
        product_description="description test",
        codigo_app="NU12345",
    )
    return mock_rest_consumer_cmdb


@pytest.mark.parametrize(
    "engagement_name, obj_cmdb",
    [
        (
            "nu0000001_Acceptance Tests",
            Cmdb(
                product_type_name="Product_type_test",
                product_name="product_name_test",
                tag_product="tag_product_test",
                product_description="description test",
                codigo_app="n1234",
            ),
        ),
        (
            "nu0000001_Acceptance Tests23",
            Cmdb(
                product_type_name="",
                product_name="",
                tag_product="",
                product_description="Orphan Product Description",
                codigo_app="n1234",
            ),
        ),
    ],
)
def test_execute(engagement_name, obj_cmdb):
    mock_rest_consumer_cmdb = get_cmdb_instance()
    request = {
        "cmdb_mapping": {
            "product_type_name": "nombreevc",
            "product_name": "nombreapp",
            "tag_product": "nombreentorno",
            "product_description": "arearesponsableti",
            "codigo_app": "CodigoApp",
        },
        "organization_url": "https://organizaciont.visualstudio.com/",
        "product_name": "test product name",
        "repository_id": "repositoryid_or_name_repository",
        "remote_config_source": "azure",
        "remote_config_repo": "repositoryid_or_name_repository",
        "remote_config_path": "/defect_dojo/cmdb_mapping.json",
        "remote_config_branch": "",
        "project_remote_config": "Vicepresidencia Servicios de Tecnología",
        "token_cmdb": "123456789",
        "host_cmdb": "http://localhost:8000",
        "cmdb_request_response": {
            "HEADERS": {
                "Content-Type": "application/json",
                "tokenkey": "tokenvalue"
            },
            "METHOD": "POST",
            "BODY": {
                "codapp": "codappvalue"
            },
            "RESPONSE": [0]
        },
        "expression": "((AUD|AP|CLD|USR|OPS|ASN|AW|NU|EUC|IS))_",
        "token_defect_dojo": "123456789101212",
        "host_defect_dojo": "http://localhost:8000",
        "scan_type": "JFrog Xray Scan",
        "engagement_name": engagement_name,
        "file": f"{DEVSECOPS_ENGINE_UTILITIES_PATH}/defect_dojo/test/files/xray_scan.json",
        "tags": ["evc"],
    }
    request: ImportScanRequest = ImportScanSerializer().load(request)
    mock_rc = mock_rest_consumer_cmdb(request, token="91qewuro9quowedafj", host="https://localhost:8000")
    # response file contect json
    file_content = [b'{"key": "value"}']
    # mock remote config source gateway
    mock_remote_config_source_gateway = MagicMock()
    mock_remote_config_source_gateway.get_remote_config.return_value = {
        "types_product": {"ORPHAN_PRODUCT_TYPE": "ORPHAN_PRODUCT_TYPE"}
    }
    mock_rc.get_product_info.return_value = obj_cmdb
    uc = CmdbUserCase(
        rest_consumer_cmdb=mock_rc,
        remote_config_source_gateway=mock_remote_config_source_gateway,
        expression=r"((AUD|AP|CLD|USR|OPS|ASN|AW|NU|EUC|IS)\d+)",
    )

    response = uc.execute(request)
    assert response.scan_type == "JFrog Xray Scan"
    assert response.code_app == "nu0000001"
    assert response.tags == ['evc']
    assert response.product_type_name in ["ORPHAN_PRODUCT_TYPE", "Product_type_test"]


@pytest.mark.parametrize("engagement_name", [("engament_name")])
def test_get_code_app(engagement_name):
    uc = CmdbUserCase(
        rest_consumer_cmdb=None, remote_config_source_gateway=None, expression=r"((AUD|AP|CLD|USR|OPS|ASN|AW|NU|EUC|IS)\d+)_"
    )
    code_app = uc.get_code_app(engagement_name)
    assert code_app == ""