"""
copy data from SQLite to MariaDB
"""
import sqlite3
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter

from config import get_logger, get_database_cursors, get_meta_data, get_scripts

parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--recreate-db', action='store_true')
parser.add_argument('--verbose', '-v', action='count', default=0)
args = parser.parse_args()


def copy_data(mariadb_conn, sqlite_conn, table_scripts):
    mariadb_cursor = mariadb_conn.cursor()
    sqlite_cursor = sqlite_conn.cursor()
    # logger.info(table_scripts)
    for table_id in scripts:
        select_statement = scripts[table_id]['select_sqlite']
        # logger.info(select_statement)
        insert_statement = scripts[table_id]['insert_mariadb']
        mariadb_cursor.execute("SET FOREIGN_KEY_CHECKS = 0")
        mariadb_cursor.execute(scripts[table_id]['truncate_mariadb'])
        try:
            sqlite_cursor.execute(select_statement)
            rows = sqlite_cursor.fetchall()
            for row in rows:
                try:
                    mariadb_cursor.execute(insert_statement, row)
                except sqlite3.Error as error:
                    logger.info('insert failed with %s\n%s\n%s', error, insert_statement, row)
            mariadb_conn.commit()
            mariadb_cursor.execute(scripts[table_id]['count'])
            (number_of_rows,) = mariadb_cursor.fetchone()
            row = sqlite_cursor.execute(scripts[table_id]['count']).fetchone()
            logger.info('%s contains %d : %d entries', scripts[table_id]['name'], number_of_rows, row[0])
        except sqlite3.Error as error:
            logger.info('select failed with %s', error)


if __name__ == '__main__':
    logger = get_logger(args.verbose)
    logger.info('kontor.copy_to_sqlite started')
    s_conn, m_conn = get_database_cursors(logger)
    meta_data_tables = get_meta_data(m_conn)
    # logger.info(meta_data_tables)
    scripts = get_scripts(meta_data_tables, logger)
    copy_data(m_conn, s_conn, scripts)
    s_conn.close()
    m_conn.close()
    logger.info('kontor.copy_to_sqlite finished')
