<?php
session_start();
require_once 'inc/functions.php';
check404();
securityCheck();
if (!isLoggedIn()) {
    header('Location: index.php');
    exit;
}

$user = getUserById($_SESSION['user_id']);
$inviteId = isset($_GET['invite_id']) ? (int)$_GET['invite_id'] : 0;
$action = $_GET['action'] ?? '';

if (!$inviteId || !in_array($action, ['accept', 'decline'])) {
    header('Location: profile.php');
    exit;
}

if ($action == 'accept') {
    $result = acceptInvite($inviteId, $user['id']);
} else {
    $result = declineInvite($inviteId, $user['id']);
}

if ($result['success']) {
    header('Location: profile.php?msg=' . urlencode($result['message']));
} else {
    header('Location: profile.php?error=' . urlencode($result['message']));
}
exit;