<?php
require_once __DIR__ . '/../../config/secure_bootstrap.php';
header('Content-Type: application/json; charset=utf-8');

try {
    $audit_id = intval($_GET['audit_id'] ?? 0);
    if ($audit_id <= 0) {
        throw new Exception('audit_id tidak valid');
    }

    $stmt = $pdo->prepare("
        SELECT 
            id, audit_id, finding_title, description, severity, recommendation, 
            responsible_unit, due_date, status, closed_at
        FROM audit_findings 
        WHERE audit_id = ?
        ORDER BY id DESC
    ");
    $stmt->execute([$audit_id]);
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    echo json_encode(['success' => true, 'data' => $data]);
} catch (Throwable $e) {
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
