import importlib
import json
from unittest.mock import patch

import pytest

import delta_barth.pipelines
from delta_barth import pipelines as pl
from delta_barth.errors import STATUS_HANDLER


@patch("delta_barth.analysis.forecast.SALES_BASE_NUM_DATAPOINTS_MONTHS", 1)
def test_sales_prognosis_pipeline(exmpl_api_sales_prognosis_resp):
    with patch(
        "delta_barth.analysis.forecast.get_sales_prognosis_data",
    ) as mock:
        mock.return_value = (exmpl_api_sales_prognosis_resp, STATUS_HANDLER.SUCCESS)
        importlib.reload(delta_barth.pipelines)
        json_export = pl.pipeline_sales_forecast(None, None)

    assert isinstance(json_export, str)
    parsed_resp = json.loads(json_export)
    assert "response" in parsed_resp
    assert "status" in parsed_resp
    assert "daten" in parsed_resp["response"]
    assert len(parsed_resp["response"]["daten"]) > 0
    assert "code" in parsed_resp["status"]
    assert parsed_resp["status"]["code"] == 0


def test_sales_prognosis_pipeline_dummy():
    json_export = pl.pipeline_sales_forecast_dummy(None, None)

    assert isinstance(json_export, str)
    parsed_resp = json.loads(json_export)
    assert "response" in parsed_resp
    assert "status" in parsed_resp
    assert "daten" in parsed_resp["response"]
    assert len(parsed_resp["response"]["daten"]) > 0
    entry = parsed_resp["response"]["daten"][0]
    assert entry["jahr"] == 2022
    assert entry["monat"] == 11
    assert entry["vorhersage"] == pytest.approx(47261.058594)
    assert "code" in parsed_resp["status"]
    assert parsed_resp["status"]["code"] == 0
