<?php
declare(strict_types=1);
require_once __DIR__ . '/../lib.php';

hw_require_method(['GET']);
$user = hw_auth_user();
if (!$user) hw_json(['message' => 'Unauthorized'], 401);

try {
    $pdo = hw_db();
    $stmt = $pdo->prepare('SELECT id, answers, created_at FROM intake_forms WHERE user_id = ? ORDER BY id DESC LIMIT 1');
    $stmt->execute([(int)$user['sub']]);
    $row = $stmt->fetch();
    if (!$row) hw_json(['intake' => null]);
    $row['answers'] = json_decode($row['answers'] ?? '[]', true);
    hw_json(['intake' => $row]);
} catch (Throwable $e) {
    hw_json(['message' => 'Server error', 'error' => $e->getMessage()], 500);
}
