<?php

namespace App\Command;

use App\Service\Prices\RedisReadService;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
    name: 'mto:prices:read',
    description: 'Liest Preise aus RedisReadService basierend auf Shop, Step und SKUs'
)]
class RedisReadCommand extends Command
{
    private RedisReadService $readService;

    public function __construct(RedisReadService $readService)
    {
        parent::__construct();
        $this->readService = $readService;
    }

    protected function configure(): void
    {
        $this
            ->addArgument('shopId', InputArgument::REQUIRED, 'Shop ID')
            ->addArgument('step', InputArgument::REQUIRED, 'Price step')
            ->addArgument('skus', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Liste von SKUs (Leerzeichen-getrennt)')
            ->addOption('akStep', null, InputOption::VALUE_OPTIONAL, 'Alternative Einkaufspreisstufe')
            ->addOption('maxPriceAge', null, InputOption::VALUE_OPTIONAL, 'Maximales Alter in Sekunden')
            ->addOption('noTax', null, InputOption::VALUE_NONE, 'Bruttopreise unterdrücken');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $shopId = (int) $input->getArgument('shopId');
        $step = (int) $input->getArgument('step');
        $skus = $input->getArgument('skus');
        $akStep = $input->getOption('akStep') !== null ? (int) $input->getOption('akStep') : null;
        $maxPriceAge = $input->getOption('maxPriceAge') !== null ? (int) $input->getOption('maxPriceAge') : null;
        $noTax = $input->getOption('noTax') ?? false;

        $data = $this->readService->getPrices($skus, $shopId, $step, $akStep, $maxPriceAge, $noTax);

        $output->writeln(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        return Command::SUCCESS;
    }
}
