<?php

namespace App\Commands;

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

class Package extends Command
{
    protected $signature = 'package {pkgAction}';

    protected $description = 'Run Command from a package.';

    public function handle(): void
    {
        $themeName = config('project.env')->get('THEME_NAME');
        $cwd = sprintf('%s/%s', config('project.themeCwd'), $themeName);

        $action = $this->argument('pkgAction');
        $cmd = null;

        if (str_starts_with($action, 'wp:block')) {
            $cmd = './vendor/bin/blocks-cli ' . $action;
        }

        if (str_starts_with($action, 'wp:theme')) {
            $cmd = './vendor/bin/base-cli ' . $action;
        }


        $cmd = match ($action) {
            'blocks.create'  => './vendor/bin/blocks-cli wp:block:create',
            'theme.generate' => './vendor/bin/base-cli wp:theme:generate',
            default          => $cmd
        };


        if(!$cmd) {
            $this->error('Command not found');
            return;
        }

        $upCommand = Docker::exec(command: $cmd, cwd: $cwd);

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

}
