<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: X-Requested-With");
header('Content-Type: application/json; charset=utf-8');

include 'db/config.php';

function check_input($par) {
    return htmlspecialchars(stripslashes(trim($par)));
}

if (empty($_POST['user_id']) || empty($_POST['auth'])) {
    echo json_encode(['status' => 'error',  'success' => false, 'message' => 'Missing required parameters']);
    exit;
}

$user_id = check_input($_POST['user_id']);
$auth    = check_input($_POST['auth']);

// Verify user
$stmt = $con->prepare("SELECT package FROM users WHERE user_id = ? AND auth = ?");
$stmt->bind_param("ss", $user_id, $auth);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

if (!$user) {
    echo json_encode(['status' => 'error', 'success' => false, 'message' => 'Invalid user ID or authentication token']);
    exit;
}

// Check package
if ($user['package'] === 'Free') {
    echo json_encode(['status' => 'error', 'success' => false, 'message' => 'This feature is not available. Upgrade plan']);
    exit;
}

// Fetch user's activities if not Free
$stmt = $con->prepare("SELECT * FROM likes_me WHERE user_id = ? ORDER BY id DESC LIMIT 100");
$stmt->bind_param("s", $user_id);
$stmt->execute();
$activityResult = $stmt->get_result();

$data = [];
while ($row = $activityResult->fetch_assoc()) {
    $data[] = $row;
}

if (!empty($data)) {
    $response = ['status' => 'Success', 'success' => true, 'data' => $data];
} else {
    $response = ['status' => 'error', 'success' => false, 'message' => 'No Likes Found', 'data' => []];
}

$con->close();
echo json_encode($response);
?>
