import sys
from datetime import datetime
import subprocess
from batch.unpaid_maestro_manager import UnpaidMaestroManager


# print('# args : {}'.format(len(sys.argv)))
# print('args : {}'.format(str(sys.argv)))
mode = 'mac'
arg_count = len(sys.argv)
if arg_count > 1:
    mode = str(sys.argv[1]).lower()


def print_items(list):
    for item in list:
        print(item)


def send_unpaid_maestro_mysqldump_to_test_server():
    backup_server_address = 'chocomae@test.chocomae.com:'
    output_directory = '/home/chocomae/unregister-user-backup/'
    if mode == 'mac':
        output_directory = ''

    today = datetime.today().strftime('%Y%m%d-%H%M%S')
    output_filename = today + '.sql'
    output_fullpath = output_directory + output_filename

    command_mysqldump = 'mysqldump --user=root --password=52napark! --single-transaction chocomae maestro'
    command_mysqldump += ' --where="DATE(NOW()) - INTERVAL 7 DAY > AcceptClausesDateTime AND ActivateStatus = 0" >'
    command_mysqldump += ' ' + output_fullpath
    print(command_mysqldump)

    command_scp = 'scp '+ output_fullpath
    command_scp += ' ' + backup_server_address + output_directory
    print(command_scp)

    subprocess.run(command_mysqldump, shell=True, check=True)
    subprocess.run(command_scp, shell=True, check=True)


# backup unpaid for 7 days maestro list
send_unpaid_maestro_mysqldump_to_test_server()

# insert log and delete from DB
unpaidMaestroMan = UnpaidMaestroManager(mode)
maestro_list = unpaidMaestroMan.get_unpaid_7days_maestro_list()
# print_items(maestro_list)
for maestro in maestro_list:
    remark = maestro['Name'] + ' / unpaid 7 days'
    if mode == "mac":
        print(remark)
    else:
        unpaidMaestroMan.insert_maestro_log(maestro['MaestroID'], remark)
        unpaidMaestroMan.delete_maestro_list(maestro['MaestroID'])
