import os
import sys
import time

import django
import openpyxl

sys.path.append('..')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'futplus.settings')
django.setup()

from accounts.models import Console, FifaAccount


def add_from_file():
    # example : key_on_consoles.xlsx
    # console	email   is_home
    # 2026	JuliusNaomi1046@outlook.com     home
    wb_obj = openpyxl.load_workbook('key_on_consoles.xlsx')
    sheet_obj = wb_obj.active
    max_row = sheet_obj.max_row
    for row_item in range(2, max_row + 1):
        console = Console.objects.filter(name=sheet_obj.cell(row=row_item, column=1).value).first()
        if not console:
            print('can not find console : ', sheet_obj.cell(row=row_item, column=1).value)
            time.sleep(1)
            continue
        fifa_account = FifaAccount.objects.filter(user_name=sheet_obj.cell(row=row_item, column=2).value).first()
        if not fifa_account:
            print('can not find fifa account : ', sheet_obj.cell(row=row_item, column=2).value)
            time.sleep(1)
            continue
        print(f'fixing console : {console} with key {fifa_account}')
        console.key_fifa_account = fifa_account
        if sheet_obj.cell(row=row_item, column=3).value == 'home':
            console.fifa_key_is_home = True
        else:
            console.fifa_key_is_home = False
        console.save()


if __name__ == '__main__':
    add_from_file()
