<?php

declare(strict_types=1);

namespace App\Command;

use Pkirillw\MaxBotApi\Api;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

abstract class AbstractCommand extends Command
{
    public function __construct(
        protected Api $api,
        protected LoggerInterface $logger,
    ) {
        parent::__construct();
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);
        try {
            return $this->doExecute($io, $input, $output);
        } catch (\Throwable $e) {
            $this->logger->error($e->getMessage(), ['exception' => $e]);
            $io->error($e->getMessage());
            return Command::FAILURE;
        }
    }

    abstract protected function doExecute(SymfonyStyle $io, InputInterface $input, OutputInterface $output): int;
}
