<?php

require_once __DIR__ . '/../../core/api_bootstrap.php';
require_once __DIR__ . '/../../core/permissions.php';

/* METHOD */
if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
    json_response(false, 'Invalid request method.', [], 405);
}

/* AUTH */
$user = authenticate($pdo);

/* RBAC */
require_permission($user, 'system_health', 'view', $pdo);

$where = [];
$params = [];

/* TENANT ISOLATION */
if (!empty($user['rsp_id'])) {
    $where[] = "c.rsp_id = ?";
    $params[] = (int)$user['rsp_id'];
}

$where_sql = '';
if ($where) {
    $where_sql = " AND " . implode(" AND ", $where);
}

/* ----------------------------------------------------------
   JOB STATUS COUNTS
---------------------------------------------------------- */
$status_sql = "
SELECT b.status, COUNT(*) AS count
FROM breakdowns b
JOIN companies c ON c.id = b.fleet_company_id
WHERE b.archived_at IS NULL
{$where_sql}
GROUP BY b.status
";

$stmt = $pdo->prepare($status_sql);
$stmt->execute($params);

$status_counts = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
    $status_counts[$row['status']] = (int)$row['count'];
}

/* ----------------------------------------------------------
   ACTIVE SP COUNT
---------------------------------------------------------- */
$sp_sql = "
SELECT COUNT(*)
FROM companies c
WHERE c.type = 'sp'
AND c.status = 'active'
" . (!empty($where) ? " AND c.rsp_id = ?" : "");

$stmt = $pdo->prepare($sp_sql);
$stmt->execute(!empty($where) ? [(int)$user['rsp_id']] : []);
$active_sp = (int)$stmt->fetchColumn();

/* ----------------------------------------------------------
   ACTIVE FLEET COUNT
---------------------------------------------------------- */
$fleet_sql = "
SELECT COUNT(*)
FROM companies c
WHERE c.type = 'fleet'
AND c.status = 'active'
" . (!empty($where) ? " AND c.rsp_id = ?" : "");

$stmt = $pdo->prepare($fleet_sql);
$stmt->execute(!empty($where) ? [(int)$user['rsp_id']] : []);
$active_fleet = (int)$stmt->fetchColumn();

/* ----------------------------------------------------------
   COMPLETED TODAY
---------------------------------------------------------- */
$today_sql = "
SELECT COUNT(*)
FROM breakdowns b
JOIN companies c ON c.id = b.fleet_company_id
WHERE DATE(b.completed_at) = CURDATE()
{$where_sql}
";

$stmt = $pdo->prepare($today_sql);
$stmt->execute($params);
$completed_today = (int)$stmt->fetchColumn();

/* ----------------------------------------------------------
   BROADCASTING NOW
---------------------------------------------------------- */
$broadcast_sql = "
SELECT COUNT(*)
FROM breakdowns b
JOIN companies c ON c.id = b.fleet_company_id
WHERE b.status = 'broadcasting'
AND b.archived_at IS NULL
{$where_sql}
";

$stmt = $pdo->prepare($broadcast_sql);
$stmt->execute($params);
$broadcasting = (int)$stmt->fetchColumn();

/* RESPONSE */
json_response(true, 'System health fetched.', [
    'status_counts'   => $status_counts,
    'active_sp'       => $active_sp,
    'active_fleet'    => $active_fleet,
    'completed_today' => $completed_today,
    'broadcasting_now'=> $broadcasting
]);