print("\nSource adHocCarrierAndMemberAdjustment.py", dot20)

# import pandas as pd
# import numpy as np


def adHocCarrierAndMemberAdjustment(start_yr, end_yr):
    print(
        "\nStarting adHocCarrierAndMemberAdjustment for BCBS rollup and membership fix"
    )

    einProdCarrName = "EINxProdxCarrier__{}_{}".format(start_yr, end_yr)
    einProdAllName = "einxprod_all"

    einProdCarr = pd.read_pickle(dataDir + "pit/" + einProdCarrName + ".pkl")
    einProdAll = pd.read_pickle(wd_delme + einProdAllName + ".pkl")

    # BCBS roll up

    blue_carriers_map = pd.read_excel(
        xwalkDir + "carriers/Blue Carriers Map.xlsx", sheet_name="BCBS Rollup"
    )
    blues = pd.unique(blue_carriers_map["BCBS_List"].tolist() + blue_carriers).tolist()

    einProdAll["Carrier_Met"] = np.where(
        einProdAll["Carrier"].isin(blues), "BCBS", einProdAll["Carrier"]
    )
    einProdCarr["Carrier_Met"] = np.where(
        einProdCarr["Carrier"].isin(blues), "BCBS", einProdCarr["Carrier"]
    )

    # Membership adjustments
    EINtoChangeMembers = "42782032"
    bcup = einProdAll[einProdAll["EIN"] == "42782032"].copy()
    einProdAll.loc[einProdAll["EIN"] == EINtoChangeMembers, "Members"] = np.round(
        einProdAll.loc[einProdAll["EIN"] == EINtoChangeMembers, "Members"] / 20
    )
    einProdCarr.loc[einProdCarr["EIN"] == EINtoChangeMembers, "Members"] = np.round(
        einProdCarr.loc[einProdCarr["EIN"] == EINtoChangeMembers, "Members"] / 20
    )

    EINtoChangeMembers = "232121745"
    bcup = einProdAll[einProdAll["EIN"] == "232121745"].copy()
    einProdAll.loc[einProdAll["EIN"] == EINtoChangeMembers, "Members"] = np.round(
        einProdAll.loc[einProdAll["EIN"] == EINtoChangeMembers, "Members"] / 20
    )
    einProdCarr.loc[einProdCarr["EIN"] == EINtoChangeMembers, "Members"] = np.round(
        einProdCarr.loc[einProdCarr["EIN"] == EINtoChangeMembers, "Members"] / 20
    )

    # one fifth #410449260 520 NC Medical KAISER FOUNDATION HEALTH PLAN, INC. 20101007173602P070003446707001 298422
    einProdAll.loc[
        (einProdAll["EIN"] == "410449260")
        & (einProdAll["Year"] == 2009)
        & (einProdAll["PN"] == "520")
        & (einProdAll["Product"] == "Medical")
        & (einProdAll["Members"] == "298422"),
        "Members",
    ] = np.round(
        einProdAll.loc[
            (einProdAll["EIN"] == "410449260")
            & (einProdAll["Year"] == 2009)
            & (einProdAll["PN"] == "520")
            & (einProdAll["Product"] == "Medical")
            & (einProdAll["Members"] == "298422"),
            "Members",
        ]
        / 5
    )
    einProdCarr.loc[
        (einProdCarr["EIN"] == "410449260")
        & (einProdCarr["Year"] == 2009)
        & (einProdCarr["PN"] == "520")
        & (einProdCarr["Product"] == "Medical")
        & (einProdCarr["Members"] == "298422"),
        "Members",
    ] = np.round(
        einProdCarr.loc[
            (einProdCarr["EIN"] == "410449260")
            & (einProdCarr["Year"] == 2009)
            & (einProdCarr["PN"] == "520")
            & (einProdCarr["Product"] == "Medical")
            & (einProdCarr["Members"] == "298422"),
            "Members",
        ]
        / 5
    )

    # one sixth #134994650 502 NY Medical DEAN HEALTH PLAN 20101015174621P040007222008001 197279
    einProdAll.loc[
        (einProdAll["EIN"] == "134994650")
        & (einProdAll["Year"] == 2009)
        & (einProdAll["PN"] == "502")
        & (einProdAll["Product"] == "Medical")
        & (einProdAll["Members"] == "197279"),
        "Members",
    ] = np.round(
        einProdAll.loc[
            (einProdAll["EIN"] == "134994650")
            & (einProdAll["Year"] == 2009)
            & (einProdAll["PN"] == "502")
            & (einProdAll["Product"] == "Medical")
            & (einProdAll["Members"] == "197279"),
            "Members",
        ]
        / 6
    )
    einProdCarr.loc[
        (einProdCarr["EIN"] == "134994650")
        & (einProdCarr["Year"] == 2009)
        & (einProdCarr["PN"] == "502")
        & (einProdCarr["Product"] == "Medical")
        & (einProdCarr["Members"] == "197279"),
        "Members",
    ] = np.round(
        einProdCarr.loc[
            (einProdCarr["EIN"] == "134994650")
            & (einProdCarr["Year"] == 2009)
            & (einProdCarr["PN"] == "502")
            & (einProdCarr["Product"] == "Medical")
            & (einProdCarr["Members"] == "197279"),
            "Members",
        ]
        / 6
    )

    # half #134994650 557 NY Medical KAISER FOUNDATION HEALTH PLANS, INC. SOUTHERN CALIFORNIA 20100729144132P040018647318001 56686
    einProdAll.loc[
        (einProdAll["EIN"] == "134994650")
        & (einProdAll["Year"] == 2009)
        & (einProdAll["PN"] == "557")
        & (einProdAll["Product"] == "Medical")
        & (einProdAll["Members"] == "56686"),
        "Members",
    ] = np.round(
        einProdAll.loc[
            (einProdAll["EIN"] == "134994650")
            & (einProdAll["Year"] == 2009)
            & (einProdAll["PN"] == "557")
            & (einProdAll["Product"] == "Medical")
            & (einProdAll["Members"] == "56686"),
            "Members",
        ]
        / 2
    )
    einProdCarr.loc[
        (einProdCarr["EIN"] == "134994650")
        & (einProdCarr["Year"] == 2009)
        & (einProdCarr["PN"] == "557")
        & (einProdCarr["Product"] == "Medical")
        & (einProdCarr["Members"] == "56686"),
        "Members",
    ] = np.round(
        einProdCarr.loc[
            (einProdCarr["EIN"] == "134994650")
            & (einProdCarr["Year"] == 2009)
            & (einProdCarr["PN"] == "557")
            & (einProdCarr["Product"] == "Medical")
            & (einProdCarr["Members"] == "56686"),
            "Members",
        ]
        / 2
    )

    bcupMmbr = einProdAll[
        einProdAll["EIN"].isin(["42782032", "410449260", "134994650"])
    ].copy()

    print("\nSaving files after adjustments")

    einProdCarr.to_pickle(dataDir + "pit/" + einProdCarrName + ".pkl")
    einProdAll.to_pickle(wd_delme + einProdAllName + ".pkl")

    del einProdAll
    del einProdCarr