import sqlite3
import django
django.setup()
from tqdm import tqdm

from bloonsa_game.models import Level, Author


def run():
    print("yes")

    with sqlite3.connect("bloonsworld_levels.db") as db:
        cursor = db.cursor()
        cursor.execute("SELECT * FROM Levels")

        for (levelNum, title, authorname, authorID, numPlays, numCompleted, rating, target, darts, data) in tqdm(cursor.fetchall()):
            authorObj = Author.objects.filter(author_id=authorID).first()
            if not authorObj:
                authorObj = Author.objects.create(author_id=authorID,
                                                  name=authorname)
                authorObj.save()

            levelObj = Level.objects.filter(level_id=levelNum).first()
            if not levelObj:
                levelObj = Level.objects.create(author=authorObj,
                                                level_id=levelNum,
                                                title=title,
                                                plays=numPlays,
                                                beats=numCompleted,
                                                rating=rating,
                                                target=target,
                                                darts=darts,
                                                data=data)
            levelObj.author = authorObj

            levelObj.save()

