<?php

namespace App\Services;

use App\Core\Database;

class PlatformFabricService
{
    public static function dashboard(int $orgId, int $conferenceId): array
    {
        $db = Database::getInstance();

        $nodes = $db->prepare("
            SELECT COUNT(*)
            FROM platform_fabric_nodes
            WHERE org_id=?
        ");
        $nodes->execute([$orgId]);

        $links = $db->prepare("
            SELECT COUNT(*)
            FROM platform_fabric_links
            WHERE source_org_id=? OR target_org_id=?
        ");
        $links->execute([$orgId, $orgId]);

        $runtime = $db->prepare("
            SELECT COUNT(*)
            FROM platform_runtime
            WHERE conference_id=?
        ");
        $runtime->execute([$conferenceId]);

        $n = (int)$nodes->fetchColumn();
        $l = (int)$links->fetchColumn();
        $r = (int)$runtime->fetchColumn();

        return [
            'nodes' => $n,
            'links' => $l,
            'runtime' => $r,
            'fabric_score' => min(100, 50 + ($n * 5) + ($l * 10) + $r),
            'state' => ($n + $l) > 0 ? 'connected' : 'local'
        ];
    }
}
