<?php

namespace App\Commands;

use App\Services\Command;
use App\Services\ProjectDoctor;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

class Doctor extends Command
{
    protected $signature = 'doctor {--path= : Project path to inspect}';

    protected $description = 'Run starter environment and project health checks';

    public function handle(ProjectDoctor $doctor): int
    {
        $projectDir = (string) ($this->option('path') ?: getcwd());
        $results = $doctor->run($projectDir);

        foreach ($results as $result) {
            $prefix = match ($result['status']) {
                'ok' => 'OK',
                'warning' => 'WARN',
                default => 'ERR',
            };

            $this->line(sprintf('[%s] %s: %s', $prefix, $result['label'], $result['message']));
        }

        return $doctor->hasErrors($results)
            ? SymfonyCommand::FAILURE
            : SymfonyCommand::SUCCESS;
    }
}
