<?php

require '../config/database.php';
session_start();

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

if (!isset($_GET['id'])) {
    echo json_encode([
        "status" => "error",
        "message" => "Concern ID required"
    ]);
    exit;
}

$id = intval($_GET['id']);

$stmt = $pdo->prepare("
SELECT
    c.*,
    s.section_name,

    u.first_name AS assigned_first,
    u.last_name AS assigned_last,

    creator.first_name AS creator_first,
    creator.last_name AS creator_last

FROM safety_concerns c

LEFT JOIN sections s
ON c.section_id = s.id

LEFT JOIN users u
ON c.assigned_user_id = u.id

LEFT JOIN users creator
ON c.created_by = creator.id

WHERE c.id = ?
");

$stmt->execute([$id]);

$concern = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$concern) {
    echo json_encode([
        "status" => "error",
        "message" => "Concern not found"
    ]);
    exit;
}

echo json_encode([
    "status" => "success",
    "data" => $concern
]);