<?php
session_start();
require_once __DIR__ . '/auth/db.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['status' => 'error', 'message' => 'POST only']);
    exit();
}

$input = json_decode(file_get_contents("php://input"), true);
if (!isset($input['email'])) {
    echo json_encode(['status' => 'error', 'message' => 'Email required']);
    exit();
}

$email = $input['email'];

$stmt = $conn->prepare("SELECT task_id FROM completed_tasks WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($task_id);

$tasks = [];
while ($stmt->fetch()) {
    $tasks[] = $task_id;
}

echo json_encode(['status' => 'success', 'completed' => $tasks]);

$stmt->close();
$conn->close();
?>