<?php

namespace App\Command;

use App\Service\Campaign\Report\PdfService;
use App\Service\Campaign\ReportService;
use Doctrine\ORM\EntityNotFoundException;
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;
use Symfony\Component\Console\Style\SymfonyStyle;

class TestPdfCommand extends Command
{
    protected static $defaultName = 'test:pdf';
    /**
     * @var ReportService
     */
    private $reportService;
    /**
     * @var PdfService
     */
    private $pdfService;

    public function __construct(ReportService $reportService, PdfService $pdfService, string $name = null)
    {
        parent::__construct($name);
        $this->reportService = $reportService;
        $this->pdfService = $pdfService;
    }

    protected function configure()
    {
        $this
            ->setDescription('Add a short description for your command')
            ->addArgument('arg1', InputArgument::REQUIRED, 'Report ID')
            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description');
    }

    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        $io = new SymfonyStyle($input, $output);
        $arg1 = $input->getArgument('arg1');

        if ($arg1) {
            $io->note(sprintf('You passed an argument: %s', $arg1));
        }

        try {
            $report = $this->reportService->get($arg1);
        } catch (EntityNotFoundException $e) {
            $io->error($e->getMessage());

            return;
        }

        try {
            $path = $this->pdfService->generate($report);
        } catch (\Exception $e) {
            $io->error($e->getMessage());

            return;
        }

        $io->success(sprintf('Path: %s', $path));
    }
}
