import asyncio
import csv
import datetime
import logging
import pathlib
import sys

from db_calls import *


async def main(argv):
    with open('manual-updates.csv', 'r') as file:
        reader = csv.reader(file)
        count = 0
        for row in reader:
            p_query = db_get('players', params=[('name', row[0]), ('cardset_id', 3)])
            if p_query:
                this_player = p_query['players'][0]

                pos_1 = row[1]
                pos_2 = row[2] if row[2] else False
                pos_3 = row[3] if row[3] else False

                db_patch('players', object_id=this_player['player_id'], params=[
                    ('pos_1', pos_1), ('pos_2', pos_2), ('pos_3', pos_3)
                ])
                count += 1

        print(f'Just updated {count} record{"s" if count != 1 else ""}')


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