import warnings

import numpy as np
import pandas as pd


def readAndConvertCSVToDbFormatDf(filePath: str, nrows: int = None) -> pd.DataFrame:
    """
    :param filePath: filename with product data
    :param nrows: limit the number of rows to read (usually used for debug)
    :return: this is an adaptor between the way the file information is saves, and how it is introduced to the database.
    It renames columns, separates min/max prices for the strong, and deals with "None" strings
    """
    fileColumnsToDbAdaptor = {
        'productURL': 'link',
        'productId': 'productId',
        'productName': 'description',
    }
    colsToReturn = ['productId', 'link', 'description', 'min_price', 'max_price']
    if nrows is None:
        dfData = pd.read_csv(filePath, encoding="utf-8-sig", compression='infer')
    else:
        dfData = pd.read_csv(filePath, encoding="utf-8-sig", nrows=nrows, compression='infer')

    dfData.drop_duplicates(inplace=True)
    dfData.reset_index(drop=True, inplace=True)

    dfData.rename(columns=fileColumnsToDbAdaptor, inplace=True)

    # todo: add sanitize description here as well
    # analyzing price array
    descriptionAsList = list(dfData['description'].values)
    descriptionSanitized = [str(desc).replace(r'&rlm', '').replace(';', ' ').replace(',', ' ').replace(r'&', ' ').replace('\\', '/') for desc in descriptionAsList]
    dfData['description'] = descriptionSanitized
    dfData['min_price'] = np.nan
    dfData['max_price'] = np.nan
    dfData.replace({'pricesArray': {'None': np.nan}}, inplace=True)
    dfData['pricesArray'] = dfData['pricesArray'].str.replace('[', '')
    dfData['pricesArray'] = dfData['pricesArray'].str.replace(']', '')
    pricesListOfStrs = dfData['pricesArray'].to_list()

    minPricesList = []
    maxPricesList = []
    for index, prices in enumerate(pricesListOfStrs):
        if prices is np.nan:
            minPricesList.append(np.nan)
            maxPricesList.append(np.nan)
        else:
            pricesAsArray = [float(price.strip()) for price in prices.split(',')]
            if len(pricesAsArray) == 1:
                minPricesList.append(pricesAsArray[0])
                maxPricesList.append(pricesAsArray[0])
            elif len(pricesAsArray) == 2:
                minPricesList.append(min(pricesAsArray))
                maxPricesList.append(max(pricesAsArray))
            else:
                raise ValueError("len(prices) > 2. This shouldn't happen. Possibly a bug: %s" % prices)

    dfData['min_price'] = minPricesList
    dfData['max_price'] = maxPricesList

    dfData = dfData.where((pd.notnull(dfData)), None)
    return dfData[colsToReturn]


