from Training_Raw_data_validataion.rawValidation import Raw_Data_validation
from DataTypeValidation_Insertion_Training.DataTypeValidationTraining import dBOperations
from DataTransform_Training.DataTransformation import dataTransform
from application_logging import logger
import os

class train_validation:

    def __init__(self, path):
        self.raw_data = Raw_Data_validation(path)
        self.dataTransform = dataTransform()
        self.dBOperation = dBOperations()

        self.file_object = open("Training_Logs/Training_Main_Log.txt", 'a+')
        self.log_writer = logger.App_Logger()

    def train_validation(self):
        try:
            self.log_writer.log(self.file_object,'Start of Validation on Files !!' )
            # extracting values from training schema
            LengthOfTimeStampInFile,LengthOfDateStampInFile, noofcolumn, column_names=self.raw_data.valuesFromSchema()

            # getting the regex defined to validate filename
            regex = self.raw_data.manualRegexCreation()

            #validating filename of training files
            self.raw_data.validationFileName(regex, LengthOfDateStampInFile,LengthOfTimeStampInFile)

            # validating if any column has all values missing
            self.raw_data.validateColumnLength(noofcolumn)

            #validating if any column has all values missing
            self.raw_data.validateMissingValuesInWholeColumn()

            self.log_writer.log(self.file_object, "Raw Data Validation Complete !!")

            self.log_writer.log(self.file_object, "Starting Data Transformation!!")
            # replacing blanks in the csv file with "Null" values to insert in table
            self.dataTransform.replaceMissingWithNull()

            self.log_writer.log(self.file_object, "DataTransformation Completed!!")

            self.log_writer.log(self.file_object, "Creating Training_Database and tables on the basis of given schema.")

            #create database with given name, if present open the connection !! Create table with columns given in shcema
            self.dBOperation.createTableDb('Training',column_names)
            self.log_writer.log(self.file_object, 'Table creation Completed!!')

            self.log_writer.log(self.file_object, "Insertion of Data into Table started !!!")

            #insert csv files in the table

            self.dBOperation.insertIntoTableGoodData('Training')
            self.log_writer.log(self.file_object, "Table creation Completed!!")
            self.log_writer.log(self.file_object, 'Insertion of Data into Table started!!!')



            # Delete the good data folder loading files in table

            self.raw_data.deleteExistingGoodDataTrainingFolder()
            self.log_writer.log(self.file_object, "Good_Data folder deleted!! ")

            self.log_writer.log(self.file_object, 'Moving bad files to Archive and deleting Bad_Data folder!!')
            # Move the bad files to archive folder

            self.raw_data.moveBadFilesToArchieveBad()
            self.log_writer.log(self.file_object, 'Bad files moved to archive!! Bad folder Deleted!!')
            self.log_writer.log(self.file_object, 'Validation Operation completed!!')
            self.log_writer.log(self.file_object, "Extracting csv file from table")
            # export data in table to csv file

            self.dBOperation.selectingDatafromtableintocsv('Training')
            self.file_object.close()

        except Exception as e:
            raise e