<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Core\Modules\ModuleLoader;
use App\Core\Registry\LicenseRegistry;

class ModuleListCommand extends Command
{
    protected $signature = 'module:list';
    protected $description = 'List all available modules with their status';

    public function handle()
    {
        $loader = new ModuleLoader();
        $modules = $loader->discover();

        if ($modules->isEmpty()) {
            $this->info('No modules found.');
            return 0;
        }

        $licensedModules = LicenseRegistry::enabledModules();

        $tableData = $modules->map(function ($module) use ($licensedModules) {
            $isLicensed = in_array($module['slug'], $licensedModules);

            return [
                'name' => $module['name'],
                'slug' => $module['slug'],
                'version' => $module['version'] ?? '1.0.0',
                'status' => $isLicensed ? '<info>Licensed</info>' : '<comment>Not Licensed</comment>',
                'requires' => implode(', ', $module['requires'] ?? []) ?: '-',
                'priority' => $module['priority'] ?? 100,
            ];
        })->toArray();

        $this->table(
            ['Name', 'Slug', 'Version', 'Status', 'Requires', 'Priority'],
            $tableData
        );

        return 0;
    }
}
