<?php

namespace App\Commands;

use App\Services\Command;
use App\Services\Docker;
use Illuminate\Support\Facades\Process;

class Run extends Command
{
    protected $signature = 'run {bin : Run a specific bin script} {args?* : Arguments}';
    protected $description = 'Run a specific bin script';

    public function handle(): void
    {

        $bin = $this->argument('bin');
        $argsString = implode(' ', $this->argument('args'));

        $cmd = match ($bin) {
            'install' => './bin/install.sh',
            'build'   => './bin/build.sh',
            'reset'   => './bin/reset.sh',
            'update'  => './bin/update.sh ' . $argsString,
            'avif'  => './bin/avif.sh ' . $argsString,
            default   => ''
        };

        $binCommand = Docker::exec(command: $cmd);

        Process::forever()->tty()->run($binCommand, function (string $type, string $output) {
            echo $output;
        });


    }
}
