import pandas as pd

if __name__ == "__main__":
    from dotenv import load_dotenv
    load_dotenv(dotenv_path='config.env')
import warnings
from loguru import logger
from scripts.core.engine.mppt_data import GetData
from scripts.core.engine.tags_data import get_tags_data
from scripts.utils.start_end_date import KairosStartEndDate
from scripts.utils.preprocessing import DataPreprocessing
from scripts.core.engine.data_training_and_inference import TrainingInference
from scripts.core.engine.final_predictions import ai_modelling
from scripts.core.engine.raw_predicted_tags import get_raw_predicted_tags
warnings.filterwarnings("ignore")


start_date, end_date, start_timestamp, end_timestamp = KairosStartEndDate().start_end_date()


def get_tag_details():
    try:
        df_raw_tags, df_predicted_tags, df_coefficients = get_raw_predicted_tags()
        logger.info(f'raw tags dataframe shape - {df_raw_tags.shape}')
        logger.info(f'predicted tags dataframe shape - {df_predicted_tags.shape}')

        df_kairos_data = get_tags_data(df_input_tags=df_raw_tags, start_timestamp=start_timestamp, end_timestamp=end_timestamp)
        logger.info(f'Shape of final df - {df_kairos_data.shape}')

        mppt_data = GetData()
        df_mppt = mppt_data.associate_inv_mppt_id(df=df_kairos_data)
        df = mppt_data.multiply_mppt_coefficients(df_mppt=df_mppt, df_coefficients=df_coefficients)
        data_preprocessing = DataPreprocessing()
        df = data_preprocessing.remove_outliers(df=df, param_list=['tilt_irradiance', 'voltage_mppt',
                                                                   'current_mppt'])

        df, df_train, df_test = data_preprocessing.train_test_split(df=df)
        get_training_inference = TrainingInference(df=df, df_train=df_train, df_test=df_test)
        ai_modelling(df_train=df_train, get_training_inference=get_training_inference,
                     df_predicted_tags=df_predicted_tags)
    except Exception as e:
        logger.exception(f'Exception - {e}')


if __name__ == '__main__':
    get_tag_details()

