import json
import sqlite3
import pickle
import uuid
from datetime import datetime
from collections import defaultdict

with sqlite3.connect("./rollbot.sqlite") as db:
    cur = db.execute("SELECT person_id, curses, blessings FROM curse_bless_score")
    karma = defaultdict(int)
    for (pid, c, b) in cur:
        karma[pid] -= c
        karma[pid] += b
    curses = {k: json.dumps({"score": v, "zero_passes": 0}) for k, v in karma.items()}
    print(curses)

    # cur = db.execute("SELECT items, group_id FROM watchlist")
    # watchlist = []
    # for (items, _) in cur:
    #     watchlist += pickle.loads(items)
    # print(watchlist)

    dead_guys = {}
    cur = db.execute("SELECT state, games, timestamp FROM hangguy_dead_guy")
    for (state, liftime, timestamp) in cur:
        timestamp = datetime.fromisoformat(timestamp)
        dead_guys[f"{uuid.uuid4().hex}-{timestamp.isoformat()}"] = json.dumps({"state": state, "lifetime": liftime, "timestamp": timestamp.timestamp()})
    print(dead_guys)


with sqlite3.connect("./rollbot.db") as db:
    db.executemany("INSERT INTO curse_score VALUES (?, ?)", list(curses.items()))
    # db.execute("INSERT INTO watchlist VALUES (?, ?)", .items())
    db.executemany("INSERT INTO dead_guy VALUES (?, ?)", list(dead_guys.items()))