from database import execute
from database import fetch_all, fetch_one


def get_user_events(user_id, date_from=None, date_to=None):
    query = """
        SELECT events.*
        FROM events
        JOIN room_members
            ON events.room_id = room_members.room_id
        WHERE room_members.user_id = ?
    """

    params = [user_id]

    if date_from:
        query += " AND events.start_time >= ?"
        params.append(date_from)

    if date_to:
        query += " AND events.start_time <= ?"
        params.append(date_to)

    query += " ORDER BY events.start_time"

    return fetch_all(
        query,
        tuple(params),
    )

def create_event(
    room_id,
    creator_id,
    type,
    title,
    description,
    start_time,
    end_time,
    visibility,
    status,
    min_people
):
    query = """
    INSERT INTO events (
        room_id,
        creator_id,
        type,
        title,
        description,
        start_time,
        end_time,
        visibility,
        status,
        min_people
    )
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
    """

    event_id = execute(
        query,
        (
            room_id,
            creator_id,
            type,
            title,
            description,
            start_time,
            end_time,
            visibility,
            status,
            min_people
        )
    )

    return event_id


# voorbeeld create_event. alleen voor deocumentatie, 
# als je echt een event toe wil voegen, moet je data
# uit de fastapi halen.
# 
#  
# event = create_event(
#     room_id=2,
#     creator_id=3,
#     type="proposal",
#     title="Kaas eten",
#     description="We gaan met z'n allen kaas eten.",
#     start_time="2026-08-10 19:00",
#     end_time="2026-08-10 21:00",
#     visibility="room",
#     status="proposed",
#     min_people=4
# )

# print("Nieuw event ID:", event)

def check_event_threshold(event_id):

    query = """
    SELECT 
        events.min_people,
        COUNT(event_signups.user_id) AS going
    FROM events

    LEFT JOIN event_signups
    ON events.id = event_signups.event_id
    AND event_signups.status = 'going'

    WHERE events.id = ?

    GROUP BY events.id
    """

    event = fetch_one(
        query,
        (event_id,)
    )

    if event["going"] >= event["min_people"]:

        execute(
            """
            UPDATE events
            SET status = 'confirmed'
            WHERE id = ?
            """,
            (event_id,)
        )
        
def delete_event(event_id):

    from database import execute

    execute(
        """
        DELETE FROM events
        WHERE id = ?
        """,
        (event_id,)
    )

def update_event(
    event_id,
    title,
    description,
    start_time,
    end_time,
    min_people,
    event_type,
    visibility,
    status,
):
    execute(
        """
        UPDATE events
        SET
            title = COALESCE(?, title),
            description = COALESCE(?, description),
            start_time = COALESCE(?, start_time),
            end_time = COALESCE(?, end_time),
            min_people = COALESCE(?, min_people),
            type = COALESCE(?, type),
            visibility = COALESCE(?, visibility),
            status = COALESCE(?, status)
        WHERE id = ?
        """,
        (
            title,
            description,
            start_time,
            end_time,
            min_people,
            event_type,
            visibility,
            status,
            event_id,
        ),
    )

def get_room_events(room_id, date_from=None, date_to=None):
    query = """
        SELECT *
        FROM events
        WHERE room_id = ?
    """

    params = [room_id]

    if date_from:
        query += " AND start_time >= ?"
        params.append(date_from)

    if date_to:
        query += " AND start_time <= ?"
        params.append(date_to)

    query += " ORDER BY start_time"

    return fetch_all(
        query,
        tuple(params),
    )   

def get_event(event_id):
    return fetch_one(
        """
        SELECT *
        FROM events
        WHERE id = ?
        """,
        (event_id,)
    )


def get_event_room_id(event_id):
    event = fetch_one(
        """
        SELECT room_id
        FROM events
        WHERE id = ?
        """,
        (event_id,)
    )

    if event is None:
        return None

    return event["room_id"]


def get_event_creator(event_id):
    event = fetch_one(
        """
        SELECT creator_id
        FROM events
        WHERE id = ?
        """,
        (event_id,)
    )

    if event is None:
        return None

    return event["creator_id"]

def confirm_event(event_id):
    execute(
        """
        UPDATE events
        SET status = 'confirmed'
        WHERE id = ?
        """,
        (event_id,),
    )


def cancel_event(event_id):
    execute(
        """
        UPDATE events
        SET status = 'cancelled'
        WHERE id = ?
        """,
        (event_id,),
    )