{% import '@lib/di.twig' as di %}
<?php

namespace Drupal\{{ machine_name }}\Drush\Commands;

{% apply sort_namespaces %}
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drush\Attributes as CLI;
  {% if services %}
use Drush\Commands\AutowireTrait;
use Drush\Formatters\FormatterTrait;
{{ di.use(services) }}
  {% endif %}
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
{% endapply %}

#[AsCommand(
  name: self::NAME,
  description: '{{ command.description }}',
  aliases: ['{{ command.alias }}'],
)]
#[CLI\Formatter(returnType: RowsOfFields::class, defaultFormatter: 'table')]
#[CLI\FieldLabels(labels: [
  'group' => 'Group',
  'token' => 'Token',
  'name' => 'Name'
])]
#[CLI\DefaultTableFields(fields: ['group', 'token', 'name'])]
#[CLI\FilterDefaultField(field: 'name')]
final class {{ class }} extends Command {
{% if services %}

  use AutowireTrait;
  use FormatterTrait;

  const NAME = '{{ command.name }}';

  /**
   * Constructs {{ class|article }} object.
   */
  public function __construct(
{{ di.signature(services) }}
  ) {
    parent::__construct();
  }
{% endif %}

  protected function configure(): void {
    $this
      ->addArgument(name: 'group', mode: InputOption::VALUE_REQUIRED, description: 'Show tokens from this group only.')
      ->addOption(name: 'example', mode: InputOption::VALUE_OPTIONAL, description: 'Example description')
      ->addUsage(usage: 'token --group=aggregator');
  }

  public function execute(InputInterface $input, OutputInterface $output): int {
    $data = $this->doExecute($input, $output);
    $this->writeFormattedOutput($input, $output, $data);
    return self::SUCCESS;
  }

  protected function doExecute(InputInterface $input, OutputInterface $output): RowsOfFields {
    $all = $this->token->getInfo();
    foreach ($all['tokens'] as $group => $tokens) {
      $group_arg = $input->getArgument('group');
      if ($group_arg && $group !== $group_arg) {
        continue;
      }
      foreach ($tokens as $key => $token) {
        $rows[] = [
          'group' => $group,
          'token' => $key,
          'name' => $token['name'],
        ];
      }
    }
    $this->logger->debug('Found {count} tokens', ['count' => count($rows)]);
    return new RowsOfFields($rows);
  }
}
