import sys
import csv

from db_calls_card_creation import *


def clean_name(name):
    return name.replace('.', '').replace("'", '').replace('-', ' ').replace('í', 'i').replace('é', 'e')\
        .replace('ñ', 'n').replace('á', 'a')


def main(argv):
    # Importing: https://docs.google.com/spreadsheets/d/1Gcgk4P8rJqX7mHO-OKPuM2FXBWNkDNZ8LNFA6DeaNck/edit#gid=72732162
    # Player Database from Columns tab
    file_name = 'data-input/players.csv'

    with open(file_name, 'r') as file:
        reader = csv.reader(file)
        all_players = []

        for row in reader:
            if row[0] != 'sba-id':
                all_players.append({
                    'name': clean_name(row[3]),
                    'fg_id': row[2],
                    'br_id': row[1],
                    'sba_id': row[0],
                    'offense_col': row[4],
                    'hand': row[5]
                })

        # TODO: will want to update this to check for existing reocrd; get_or_create ?
        with db.atomic():
            for batch in chunked(all_players, 50):
                Player.insert_many(batch).on_conflict_ignore().execute()
        db.close()

        print(f'Processed {len(all_players)} players')


if __name__ == '__main__':
    main(sys.argv[1:])
