from random import choices
import sqlite3
import config
from custom_types import Character

DB_PATH = config.DB_PATH
CONNECTION: sqlite3.Connection
CURSOR: sqlite3.Cursor


def connect() -> None:
    '''Creates a connection to the database'''
    print('Connecting to the database...')
    global CONNECTION
    global CURSOR
    CONNECTION = sqlite3.connect(DB_PATH, autocommit=True)
    CONNECTION.row_factory = sqlite3.Row
    CURSOR = CONNECTION.cursor()


def get_random_character() -> Character | None:
    ''' Gets a random character from the database'''
    CURSOR.execute('SELECT * FROM characters')
    characters = CURSOR.fetchall()

    if not characters:
        return None

    weights = [config.RARITY_TO_WEIGHT[c['rarity']] for c in characters]
    chosen = choices(characters, weights=weights, k=1)[0]

    return {
        'id': chosen['id'],
        'name': chosen['name'],
        'rarity': chosen['rarity'],
        'weight': config.RARITY_TO_WEIGHT[chosen['rarity']],
        'image_url': chosen['file_id']
    }

def get_player(username: str) -> int:
    '''Retrieve a player ID by username, or return None if not found.'''
    CURSOR.execute('SELECT id FROM users WHERE username = ?', (username,))
    user = CURSOR.fetchone()
    if user:
        return int(user[0])

def insert_player(username: str) -> int:
    '''Insert a new player with default has_rolled = False and return their user ID.'''
    CURSOR.execute(
        'INSERT INTO users (username, has_rolled) VALUES (?, ?)',
        (username, False)
    )
    return CURSOR.lastrowid

def delete_player(username: str) -> bool:
    '''Permanently deletes a user and all their pulls.'''
    CURSOR.execute(
        'SELECT id FROM users WHERE username = ?',
        (username,)
    )
    user = CURSOR.fetchone()

    user_id = user[0]

    # Delete pulls
    CURSOR.execute(
        'DELETE FROM pulls WHERE user_id = ?',
        (user_id,)
    )

    # Delete user
    CURSOR.execute(
        'DELETE FROM users WHERE id = ?',
        (user_id,)
    )

    return True



def insert_character(
        name: str, rarity: int, weight: float, file_id: str) -> int:
    '''Inserts a character'''
    CURSOR.execute(
        'INSERT INTO characters (name, rarity, weight, file_id) VALUES \
(?, ?, ?, ?)',
        (name, rarity, weight, file_id)
    )
    character_id = CURSOR.lastrowid
    return character_id if character_id else 0


def insert_pull(user_id: int, character_id: int) -> None:
    '''Creates a pull in the database'''
    CURSOR.execute(
        'INSERT INTO pulls (user_id, character_id) VALUES (?, ?)',
        (user_id, character_id)
    )


def get_last_rolled_at(user_id: int) -> int:
    '''Gets the timestamp when the user last rolled'''
    CURSOR.execute(
        "SELECT timestamp FROM pulls WHERE user_id = ? ORDER BY timestamp \
DESC",
        (user_id,))
    row = CURSOR.fetchone()
    return row[0] if row else 0


def get_config(key: str) -> str:
    '''Reads the value for a specified config key from the db'''
    CURSOR.execute("SELECT value FROM config WHERE key = ?", (key,))
    row = CURSOR.fetchone()
    return row[0] if row else ''


def set_config(key: str, value: str) -> None:
    '''Writes the value for a specified config key to the db'''
    CURSOR.execute("INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)",
                   (key, value))