<?php

namespace App\Services;

use App\Core\Database;

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

        $map = [
            'heritage' => "SELECT COUNT(*) FROM platform_heritage_records WHERE org_id=?",
            'dna' => "SELECT COUNT(*) FROM platform_institution_dna WHERE org_id=?",
            'legacy' => "SELECT COUNT(*) FROM platform_legacy_records WHERE org_id=?",
            'stewardship' => "SELECT COUNT(*) FROM platform_stewardship WHERE org_id=?"
        ];

        $out = [];

        foreach ($map as $k => $sql) {
            $stmt = $db->prepare($sql);
            $stmt->execute([$orgId]);
            $out[$k] = (int)$stmt->fetchColumn();
        }

        $out['preservation'] = min(
            100,
            55 + ($out['heritage'] * 3) + ($out['dna'] * 4) + ($out['legacy'] * 2)
        );

        $out['state'] =
            $out['preservation'] >= 85
            ? 'preserved'
            : 'collecting';

        return $out;
    }
}
