<?php
// Prüft, ob ein Benutzer im Abwesenheitsmodus ist
function is_user_on_vacation($conn, $user_id, $meeting_date)
{
    $stmt = mysqli_prepare($conn, "
        SELECT 1 FROM vacations 
        WHERE user_id = ? 
        AND ? BETWEEN start_date AND end_date
        LIMIT 1
    ");
    if (!$stmt) return false;
    mysqli_stmt_bind_param($stmt, "is", $user_id, $meeting_date);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    $found = mysqli_fetch_assoc($result);
    mysqli_stmt_close($stmt);
    return $found !== null;
}

// Automatisch ablehnen, wenn Benutzer im Urlaub ist und nicht zugesagt hat
function auto_decline_if_on_vacation($conn, $meeting_id, $user_id, $meeting_date)
{
    if (!is_user_on_vacation($conn, $user_id, $meeting_date)) {
        return null; // Nicht im Urlaub → nichts tun
    }

    // Prüfen, ob bereits ein Eintrag existiert
    $check_sql = "SELECT rsvp_status FROM meeting_teilnehmer WHERE meeting_id = ? AND user_id = ?";
    $check_stmt = mysqli_prepare($conn, $check_sql);
    mysqli_stmt_bind_param($check_stmt, "ii", $meeting_id, $user_id);
    mysqli_stmt_execute($check_stmt);
    $existing = mysqli_fetch_assoc(mysqli_stmt_get_result($check_stmt));
    mysqli_stmt_close($check_stmt);

    // Wenn bereits "accepted", respektieren wir das
    if ($existing && $existing['rsvp_status'] === 'accepted') {
        return 'accepted';
    }

    // Sonst: automatisch ablehnen
    if ($existing) {
        // Update
        $upd = mysqli_prepare($conn, "UPDATE meeting_teilnehmer SET rsvp_status = 'declined', attended = 0 WHERE meeting_id = ? AND user_id = ?");
        mysqli_stmt_bind_param($upd, "ii", $meeting_id, $user_id);
        mysqli_stmt_execute($upd);
        mysqli_stmt_close($upd);
    } else {
        // Insert
        $ins = mysqli_prepare($conn, "INSERT INTO meeting_teilnehmer (meeting_id, user_id, rsvp_status, attended) VALUES (?, ?, 'declined', 0)");
        mysqli_stmt_bind_param($ins, "ii", $meeting_id, $user_id);
        mysqli_stmt_execute($ins);
        mysqli_stmt_close($ins);
    }

    return 'declined';
}

// Ursprüngliche RSVP-Handler-Funktion
function handle_rsvp_action($conn, $meeting_id, $user_id, $action)
{
    $rsvp_status_value = null;
    $attended_value = 0;

    if ($action === 'accept') {
        $rsvp_status_value = 'accepted';
        $attended_value = 1;
    } elseif ($action === 'decline') {
        $rsvp_status_value = 'declined';
        $attended_value = 0;
    } elseif ($action === 'maybe') {
        $rsvp_status_value = 'maybe';
        $attended_value = 0;
    }

    if ($rsvp_status_value === null) return;

    $check_sql = "SELECT rsvp_status FROM meeting_teilnehmer WHERE meeting_id = ? AND user_id = ?";
    $check_stmt = mysqli_prepare($conn, $check_sql);
    $existing = null;
    if ($check_stmt) {
        mysqli_stmt_bind_param($check_stmt, "ii", $meeting_id, $user_id);
        mysqli_stmt_execute($check_stmt);
        $existing = mysqli_fetch_assoc(mysqli_stmt_get_result($check_stmt));
        mysqli_stmt_close($check_stmt);
    }

    if ($existing) {
        $update_sql = "UPDATE meeting_teilnehmer SET rsvp_status = ?, attended = ? WHERE meeting_id = ? AND user_id = ?";
        $update_stmt = mysqli_prepare($conn, $update_sql);
        if ($update_stmt) {
            mysqli_stmt_bind_param($update_stmt, "siii", $rsvp_status_value, $attended_value, $meeting_id, $user_id);
            mysqli_stmt_execute($update_stmt);
            mysqli_stmt_close($update_stmt);
        }
    } else {
        $insert_sql = "INSERT INTO meeting_teilnehmer (meeting_id, user_id, attended, rsvp_status) VALUES (?, ?, ?, ?)";
        $insert_stmt = mysqli_prepare($conn, $insert_sql);
        if ($insert_stmt) {
            mysqli_stmt_bind_param($insert_stmt, "iiis", $meeting_id, $user_id, $attended_value, $rsvp_status_value);
            mysqli_stmt_execute($insert_stmt);
            mysqli_stmt_close($insert_stmt);
        }
    }
}

function get_user_rsvp_status($conn, $meeting_id, $user_id)
{
    $stmt = mysqli_prepare($conn, "SELECT rsvp_status FROM meeting_teilnehmer WHERE meeting_id = ? AND user_id = ?");
    if (!$stmt) return null;
    mysqli_stmt_bind_param($stmt, "ii", $meeting_id, $user_id);
    mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
    mysqli_stmt_close($stmt);
    return $row ? $row['rsvp_status'] : null;
}

function get_attendee_lists($conn, $meeting_id)
{
    $stmt = mysqli_prepare($conn, "SELECT t.rsvp_status, u.username FROM meeting_teilnehmer t LEFT JOIN users u ON t.user_id = u.id WHERE t.meeting_id = ?");
    if (!$stmt) return ['accepted_users' => [], 'declined_users' => [], 'maybe_users' => [], 'total_accepted' => 0, 'total_declined' => 0, 'total_maybe' => 0];

    mysqli_stmt_bind_param($stmt, "i", $meeting_id);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);

    $accepted = $declined = $maybe = [];
    while ($row = mysqli_fetch_assoc($result)) {
        $username = htmlspecialchars($row['username']);
        switch ($row['rsvp_status']) {
            case 'accepted':
                $accepted[] = $username;
                break;
            case 'declined':
                $declined[] = $username;
                break;
            case 'maybe':
                $maybe[] = $username;
                break;
        }
    }
    mysqli_stmt_close($stmt);

    return [
        'accepted_users' => $accepted,
        'declined_users' => $declined,
        'maybe_users' => $maybe,
        'total_accepted' => count($accepted),
        'total_declined' => count($declined),
        'total_maybe' => count($maybe)
    ];
}
