<?php
// ─── GET /api/teams/list.php ─────────────────────────────────────────────
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();

$rows = $db->rows(
    "SELECT t.*,
            (SELECT COUNT(*) FROM users u WHERE u.team_id = t.name) AS member_count,
            (SELECT COUNT(*) FROM jobcards j WHERE j.team_assigned_id = t.record_id
             AND j.jc_current_status NOT IN ('COMPLETE')) AS active_jobcards
     FROM   teams t
     ORDER BY t.status DESC, t.name ASC"
);

Response::ok($rows);