import pytest

from delta_barth.analysis import forecast as fc


def test_sales_per_customer_success(sales_data):
    customer_id = 1133
    err, res = fc.sales_per_customer(sales_data, customer_id)

    assert err == 0
    assert res is not None


def test_sales_per_customer_too_few_data_points(sales_data):
    customer_id = 1000
    err, res = fc.sales_per_customer(sales_data, customer_id)

    assert err == 1
    assert res is None


def test_parse_api_resp_to_df(exmpl_api_sales_prognosis_resp):
    resp = exmpl_api_sales_prognosis_resp
    df = fc.parse_api_resp_to_df(resp)
    features = set(
        (
            "artikelId",
            "warengruppeId",
            "firmaId",
            "betrag",
            "menge",
            "buchungsDatum",
        )
    )
    assert all(col in features for col in df.columns)


def test_preprocess_sales_per_customer(exmpl_api_sales_prognosis_resp):
    resp = exmpl_api_sales_prognosis_resp
    feat_mapping: dict[str, str] = {
        "artikelId": "artikel_refid",
        "firmaId": "firma_refid",
        "betrag": "betrag",
        "menge": "menge",
        "buchungsDatum": "buchungs_datum",
    }
    target_features: frozenset[str] = frozenset(
        (
            "firma_refid",
            "betrag",
            "buchungs_datum",
        )
    )
    df = fc.preprocess_sales_per_customer(
        resp,
        feature_map=feat_mapping,
        target_features=target_features,
    )
    assert len(df.columns) == 5
    assert any(feat not in df.columns for feat in feat_mapping.keys())
