from db import get_connection


def create_event(
    match_id,
    side,
    type_,
    player_name,
    minute,
    seconds,
    meta=None,
    player_id=None,
    player_out_id=None,
    player_in_id=None,
):
    query = """
    INSERT INTO match_events_ui (
        match_id,
        side,
        type,
        player_name,
        minute,
        seconds,
        meta,
        player_id,
        player_out_id,
        player_in_id,
        created_at
    )
    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, NOW())
    RETURNING id;
    """

    conn = get_connection()
    try:
        with conn.cursor() as cur:
            cur.execute(
                query,
                (
                    match_id,
                    side,
                    type_,
                    player_name,
                    minute,
                    seconds,
                    meta,
                    player_id,
                    player_out_id,
                    player_in_id,
                ),
            )
            event_id = cur.fetchone()[0]
        conn.commit()
        return event_id
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()


def get_events(match_id):
    query = """
    SELECT
        id,
        side,
        type,
        player_name,
        minute,
        seconds,
        meta,
        player_id,
        player_out_id,
        player_in_id
    FROM match_events_ui
    WHERE match_id = %s
    ORDER BY seconds, id;
    """

    conn = get_connection()
    try:
        with conn.cursor() as cur:
            cur.execute(query, (match_id,))
            rows = cur.fetchall()
            return [
                {
                    "id": r[0],
                    "side": r[1],
                    "type": r[2],
                    "player_name": r[3],
                    "minute": r[4],
                    "seconds": r[5],
                    "meta": r[6],
                    "player_id": r[7],
                    "player_out_id": r[8],
                    "player_in_id": r[9],
                }
                for r in rows
            ]
    finally:
        conn.close()


def update_event(
    event_id,
    side,
    type_,
    player_name,
    minute,
    seconds,
    meta=None,
    player_id=None,
    player_out_id=None,
    player_in_id=None,
):
    query = """
    UPDATE match_events_ui
    SET
        side = %s,
        type = %s,
        player_name = %s,
        minute = %s,
        seconds = %s,
        meta = %s,
        player_id = %s,
        player_out_id = %s,
        player_in_id = %s
    WHERE id = %s;
    """

    conn = get_connection()
    try:
        with conn.cursor() as cur:
            cur.execute(
                query,
                (
                    side,
                    type_,
                    player_name,
                    minute,
                    seconds,
                    meta,
                    player_id,
                    player_out_id,
                    player_in_id,
                    event_id,
                ),
            )
        conn.commit()
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()


def delete_event(event_id):
    query = "DELETE FROM match_events_ui WHERE id = %s;"

    conn = get_connection()
    try:
        with conn.cursor() as cur:
            cur.execute(query, (event_id,))
        conn.commit()
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()


def clear_events(match_id):
    query = "DELETE FROM match_events_ui WHERE match_id = %s;"

    conn = get_connection()
    try:
        with conn.cursor() as cur:
            cur.execute(query, (match_id,))
        conn.commit()
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()
