<?php

namespace MintyPHP\Console\Commands\Module;

final class DeactivateCommand extends AbstractModuleCommand
{
    public function name(): string
    {
        return 'module:deactivate';
    }

    public function description(): string
    {
        return 'Run a module\'s deactivation handler';
    }

    public function usage(): string
    {
        return <<<'USAGE'
        Usage: php bin/console module:deactivate <module-id> [options]

        Options:
          --confirm    Execute the deactivation handler
          --dry-run    Validate the handler without executing

        The module does not need to be in the enabled list.
        USAGE;
    }

    public function execute(array $args, array $options): int
    {
        $moduleId = $args[0] ?? '';
        if ($moduleId === '') {
            fwrite(STDERR, $this->usage() . "\n");
            return 1;
        }

        $result = $this->moduleRunner()->deactivate(
            $moduleId,
            (bool) ($options['confirm'] ?? false),
            (bool) ($options['dry-run'] ?? false)
        );

        $stream = $result['exit_code'] === 0 ? STDOUT : STDERR;
        fwrite($stream, $result['message'] . PHP_EOL);

        return (int) $result['exit_code'];
    }
}
