<?php

namespace App\Commands;

use App\Services\Command;
use App\Services\Docker;
use Illuminate\Support\Facades\Process;
use function Laravel\Prompts\select;

class Composer extends Command
{
    protected $signature = 'composer 
        {commands* : The yarn command to run}';

    protected $description = 'Run yarn within the container';

    public function handle() : void
    {

        $cmd = $this->argument('commands');
        $themeName = config('project.env')->get('THEME_NAME');

        $targetDir = select(
            label: 'Where do you want to run the command?',
            options: ['theme' => 'in Theme', 'root' => 'in Root'],
            default: 'theme'
        );

        $cwd = match ($targetDir) {
            'root'  => config('project.publicCwd'),
            default => sprintf('%s/%s', config('project.themeCwd'), $themeName),
        };

        $shellCommand = Docker::exec(
            command: sprintf('composer %s', implode(' ', $cmd)),
            cwd: $cwd
        );

        Process::run($shellCommand, function (string $type, string $output) {
            echo $output;
        });
    }

}
