import gspread
from oauth2client.service_account import ServiceAccountCredentials
import os.path
from os import path

import pandas as pd
import pickle
import numpy as np

import options

def load_debt_price_data(debt_price_source: options.DebtPriceSource):
    """
    Load the debt price data, either from an external data source, or generated by the debt price regression model.
    
    If the DebtPriceSource is set to EXTERNAL, check if there are existing Google spreadsheet credentials,
    otherwise load the debt price data from a local CSV.
    
    Tabular data format example:
    '''csv
    timestamp, seconds_passed, price_move
    2020-09-07 10:05:33, 0, 1
    2020-09-07 11:31:58, 5185, 0.9993389888
    '''

    If the DebtPriceSource is set to DEBT_MARKET_MODEL, load the scikit-learn regression model trained on historical MakerDAO Dai dataset,
    and return the debt price data as a Pandas dataframe.
    """
    
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import mean_squared_error, r2_score
    from autosklearn.regression import AutoSklearnRegressor
    from autosklearn.metrics import mean_squared_error as auto_mean_squared_error

    test_dfs = []
    if debt_price_source == options.DebtPriceSource.EXTERNAL.value:
        if path.exists('./credentials/spreadsheet-credentials.json'):
            scope = [
                'https://spreadsheets.google.com/feeds',
                'https://www.googleapis.com/auth/drive'
            ]

            credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials/spreadsheet-credentials.json', scope) # Your json file here
            gc = gspread.authorize(credentials)

            test_spreadsheet = gc.open('debt-price-test-data')
            print(gc.list_spreadsheet_files())

            worksheet_list = test_spreadsheet.worksheets()
            test_dfs = [pd.DataFrame(ws.get_all_values()[1:], columns=ws.get_all_values()[0]).copy() for ws in worksheet_list]
        else:
            debt_price_dataframe = pd.read_csv('./tests/data/default_debt_price_source.csv')
            test_dfs = [debt_price_dataframe]
    elif debt_price_source == options.DebtPriceSource.DEBT_MARKET_MODEL.value:
        # Load the historical debt market dataset
        debt_market_df = pd.read_csv('models/market_model/data/debt_market_df.csv', index_col='date', parse_dates=True)
        # Load the scikit-learn regression model
        loaded_model = pickle.load(open('models/market_model/debt_price_estimator.pickle', 'rb'))
        
        # The set of market data features
        features = ['beta', 'Q', 'v_1', 'v_2 + v_3', 
                    'rho_star', 'C_star',
                    'D_1', 'u_1', 'u_2', 'u_3', 'u_2 + u_3', 
                    'D_2', 'w_1', 'w_2', 'w_3', 'w_2 + w_3',
                    'D']

        # Select the subset of the debt market dataset used in the regression model debt price prediction
        data_to_predict = debt_market_df[features]
        loaded_model_predictions = loaded_model.predict(data_to_predict)

        # Create a Pandas dataframe of the model predictions
        df = pd.DataFrame(loaded_model_predictions)
        df['debt_price'] = df[0] # Set the debt_price column to the debt price prediction dataframe column zero
        df['price_move'] = df['debt_price'].diff() # Calculate and store the price_move as the diff of the debt_price 
        df['price_move'][0] = df['debt_price'][0] - 1 # Remove NaN, by setting first price_move to first debt_price minus initial state of 1 Dai
        df.insert(0, 'seconds_passed', 24 * 3600) # Create a column called seconds_passed, and set it to 1 day in seconds (the period of the historical dataset)
        test_dfs = [df]
    
    return test_dfs
