<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Console\Commands;

use PpPhp\Core\Platform\Console\AbstractCommand;
use PpPhp\Core\Platform\Console\ConsolePath;
use PpPhp\Core\Platform\DI\Compiler\Factory\FactoryCachePaths;
use PpPhp\Core\Platform\DI\Integration\PlatformContainerRuntime;

final class ContainerStatsCommand extends AbstractCommand
{
    public function name(): string { return 'container:stats'; }
    public function description(): string { return 'Show integrated compiled DI container statistics.'; }

    public function handle(array $arguments = []): int
    {
        $base = ConsolePath::basePath();
        $file = $base . '/storage/cache/di/compiled-definitions.php';
        $data = is_file($file) ? require $file : ['definitions' => [], 'metadata' => []];
        $runtimeSummary = (new PlatformContainerRuntime($base))->summary();
        $factoryFiles = is_dir(FactoryCachePaths::directory($base)) ? glob(FactoryCachePaths::directory($base) . '/*.php') : [];

        $this->title('PPphp DI Container Stats');
        $this->line('Compiled: ' . (is_file($file) ? 'YES' : 'NO'));
        $this->line('Runtime definitions: ' . $runtimeSummary['definitions']);
        $this->line('Compiled definitions: ' . count($data['definitions'] ?? []));
        $this->line('Factories generated: ' . (($data['metadata']['factoryGeneration']['generated'] ?? 0)));
        $this->line('Factory files on disk: ' . count($factoryFiles ?: []));
        $this->line('Memory: ' . memory_get_usage(true));
        return 0;
    }
}
