import asyncio
import sys

from db_calls import get_player_data, db_post
from db_calls_card_creation import ScoutPlayer


async def main(argv):
    payload = {'players': []}
    skipped = []
    for x in ScoutPlayer.select():
        p_data = get_player_data(x.br_id, id_type='bbref')
        if p_data is not None:
            payload['players'].append({
                'first_name': p_data['name_first'].title(),
                'last_name': p_data['name_last'].title(),
                'key_fangraphs': p_data['key_fangraphs'],
                'key_bbref': p_data['key_bbref'],
                'key_retro': p_data['key_retro'],
                'key_mlbam': p_data['key_mlbam'],
                'offense_col': x.offense_col
            })
        else:
            skipped.append(f'{x.name} / bbref: {x.br_id}')

    resp = await db_post('mlbplayers', payload=payload)
    print(resp)
    print(", ".join(skipped))


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