# Import libraries
import sys
import pandas as pd
from sqlalchemy import create_engine


def load_data(messages_filepath, categories_filepath):
    """
    Load the data from the disaster response csvs

    Parameters:
		messages_filepath (str): 	Path to messages csv
		categories_filepath (str): 	Path to categories csv


	Returns:
        Dataframe: Merged data 
    """
    messages = pd.read_csv(messages_filepath)
    categories = pd.read_csv(categories_filepath)
    df = pd.merge(messages,categories,on='id')
    return df


def clean_data(df):
    """
    Cleans the categories

    Parameters:
		df (DataFrame): Messy DataFrame

	Returns:
        Dataframe: Cleaned dataframe
    """

    categories = df['categories'].str.split( pat=';', expand=True)
    row = categories.iloc[[1]]

    category_colnames = row.apply(lambda x : x.values[0].split("-")[0])
    categories.columns = category_colnames

    for column in categories:
        categories[column] = categories[column].astype(str).str[-1:]
        categories[column] = categories[column].astype(int)
        categories[column] = categories[column].map(lambda x: 1 if x > 1 else x)
		
    df.drop(['categories'], axis=1, inplace=True)
    df = df = pd.concat([df,categories], axis=1)
    df.drop_duplicates(inplace=True)
	
    return df


def save_data(df, database_filename):
    """
    Saves the DataFrame 

    Parameters:
		df (DataFrame): 				Cleaned DataFrame
		database_filename (DataFrame): 	Path to the SQLite Database

    """
    engine = create_engine('sqlite:///' + database_filename + '.db')
    df.to_sql(database_filename, engine, index=False, if_exists='replace')


def main():
    if len(sys.argv) == 4:

        messages_filepath, categories_filepath, database_filepath = sys.argv[1:]

        print('Loading data...\n    MESSAGES: {}\n    CATEGORIES: {}'
              .format(messages_filepath, categories_filepath))
        df = load_data(messages_filepath, categories_filepath)

        print('Cleaning data...')
        df = clean_data(df)
        
        print('Saving data...\n    DATABASE: {}'.format(database_filepath))
        save_data(df, database_filepath)
        
        print('Cleaned data saved to database!')
    
    else:
        print('Please provide the filepaths of the messages and categories '\
              'datasets as the first and second argument respectively, as '\
              'well as the filepath of the database to save the cleaned data '\
              'to as the third argument. \n\nExample: python process_data.py '\
              'disaster_messages.csv disaster_categories.csv '\
              'DisasterResponse.db')


if __name__ == '__main__':
    main()
[(19, 'pandas.read_csv', 'pd.read_csv', True, 'import pandas as pd\n'), (20, 'pandas.read_csv', 'pd.read_csv', True, 'import pandas as pd\n'), (21, 'pandas.merge', 'pd.merge', True, 'import pandas as pd\n'), (48, 'pandas.concat', 'pd.concat', True, 'import pandas as pd\n'), (63, 'sqlalchemy.create_engine', 'create_engine', False, 'from sqlalchemy import create_engine\n')]