from database import execute, fetch_one, fetch_all


def respond_to_event(event_id, user_id, status):

    query = """
    INSERT INTO event_signups
    (event_id, user_id, status)
    VALUES (?, ?, ?)

    ON CONFLICT(event_id, user_id)
    DO UPDATE SET status = excluded.status
    """

    return execute(
        query,
        (
            event_id,
            user_id,
            status
        )
    )


def count_confirmations(event_id):

    query = """
    SELECT COUNT(*) as amount
    FROM event_signups
    WHERE event_id = ?
    AND status = 'going'
    """

    result = fetch_one(
        query,
        (event_id,)
    )

    return result["amount"]

def get_event_responses(event_id):

    return fetch_all(
        """
        SELECT
            users.id,
            users.username,
            event_signups.status
        FROM event_signups
        JOIN users
        ON users.id = event_signups.user_id
        WHERE event_signups.event_id = ?
        """,
        (event_id,)
    )

    
def get_event_responses(event_id):
    return fetch_all(
        """
        SELECT *
        FROM event_signups
        WHERE event_id = ?
        """,
        (event_id,)
    )