<?php

namespace App\Commands;

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

class Shell extends Command
{
    protected $signature = 'shell';

    protected $description = 'Start a shell in the docker environment';

    public function handle(): void
    {
        $themeName = config('project.env')->get('THEME_NAME');

        $this->info('Starting Shell');


        $targetDir = select(
            label: 'Where do you want to enter the shell?',
            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(cwd: $cwd);

        Process::forever()->tty()->run($shellCommand, function (string $type, string $output) {
            $this->line($output);
        });
    }

}
