<?php

namespace App\Services;

use App\Core\Database;

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

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

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

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

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

        $out = [
            'legacy' => (int)$legacy->fetchColumn(),
            'continuity' => (int)$continuity->fetchColumn(),
            'memory' => (int)$memory->fetchColumn(),
            'roadmaps' => (int)$roadmaps->fetchColumn(),
        ];

        $out['score'] = min(
            100,
            55 + ($out['legacy'] * 3) + ($out['continuity'] * 2) + ($out['memory'] / 2)
        );

        $out['state'] = $out['score'] >= 85 ? 'continuous' : 'building';

        return $out;
    }
}
