<?php

namespace App\Console\Commands\Addon;

use App\Core\Addon\Doctor;
use Illuminate\Console\Command;

class AddonDoctorCommand extends Command
{
    protected $signature = 'addon:doctor {name?}';
    protected $description = 'Run health checks on addon modules';

    public function handle(Doctor $doctor): int
    {
        $results = $doctor->check($this->argument('name'));
        if (empty($results)) {
            $this->info('No addons to check.');
            return self::SUCCESS;
        }

        $rows = array_map(fn ($r) => [
            $r['addon'],
            strtoupper($r['level']),
            $r['check'],
            $r['message'],
        ], $results);
        $this->table(['Addon', 'Level', 'Check', 'Message'], $rows);

        $hasError = collect($results)->contains(fn ($r) => $r['level'] === 'error');
        return $hasError ? 1 : self::SUCCESS;
    }
}
