<?php
// ─── GET /api/leads/get.php?record_id=X ──────────────────────────────────
define('ROOT', dirname(__DIR__, 2));
require_once ROOT . '/core/DB.php';
require_once ROOT . '/core/Response.php';
require_once ROOT . '/core/Auth.php';

Auth::require();
$db = DB::get();

$record_id = (int) ($_GET['record_id'] ?? 0);
if (!$record_id) Response::error('record_id is required.');

$row = $db->row(
    "SELECT wr.*, u.username AS created_by, t.name AS team_name
     FROM   work_requests wr
     LEFT JOIN users u ON u.record_id = wr.user_id
     LEFT JOIN teams t ON t.record_id = wr.team_id
     WHERE  wr.record_id = ?",
    [$record_id]
);

if (!$row) Response::error('Record not found.', 404);

Response::ok($row);