<?php
declare(strict_types=1);
namespace App\Services;
use App\Core\Database; use App\Services\Pipeline\PipelineOrchestrator; use PDO; use RuntimeException;
final class UpdateValidationEngineService {
 public function dashboard():array{$p=Database::connection()->prepare("SELECT id,nom_package,version_package,statut,importe_at,checksum_sha256,message FROM cda_update_packages ORDER BY id DESC LIMIT 60");$p->execute();$r=Database::connection()->prepare("SELECT * FROM system_package_pipeline_runs ORDER BY id DESC LIMIT 50");$r->execute();$s=Database::connection()->prepare("SELECT * FROM system_package_pipeline_steps WHERE run_id IN (SELECT id FROM system_package_pipeline_runs ORDER BY id DESC LIMIT 15) ORDER BY run_id DESC,step_order,id");$s->execute();$by=[];foreach($s->fetchAll(PDO::FETCH_ASSOC)?:[] as $x)$by[(int)$x['run_id']][]=$x;return ['packages'=>$p->fetchAll(PDO::FETCH_ASSOC)?:[],'runs'=>$r->fetchAll(PDO::FETCH_ASSOC)?:[],'steps_by_run'=>$by,'generated_at'=>date('Y-m-d H:i:s')];}
 public function simulate(int $packageId,?int $userId):array{$s=Database::connection()->prepare('SELECT fichier_original FROM cda_update_packages WHERE id=:id LIMIT 1');$s->execute(['id'=>$packageId]);$a=$s->fetchColumn();if(!is_string($a)||$a==='')throw new RuntimeException('Package introuvable.');return (new PipelineOrchestrator())->execute(BASE_PATH.'/storage/updates/packages/'.basename($a),$packageId,$userId,'simulation',true);}
}
