<?php
declare(strict_types=1);

namespace Autogedal\Extend\Console\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Autogedal\Extend\Model\Product\SpecsDescriptionManager;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Model\Product\Action as ProductAction;
use Magento\Framework\App\State;
use Magento\Framework\App\Area;
use Magento\Framework\Console\Cli;

class UpdateSpecsDescriptionCommand extends Command
{
    public function __construct(
        private readonly SpecsDescriptionManager $specsManager,
        private readonly CollectionFactory $productCollectionFactory,
        private readonly ProductAction $productAction,
        private readonly State $appState
    ) {
        parent::__construct();
    }


    protected function configure(): void
    {
        $this->setName('autogedal:product:update_specs_description');
        $this->setDescription('Updates description with specs for all products in category 59 (Carlige remorcare)');
    }


    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        try {
            $this->appState->emulateAreaCode(
                Area::AREA_ADMINHTML,
                [$this, 'processProducts'],
                [$output]
            );
        } catch (\Exception $e) {
            $output->writeln('<error>' . $e->getMessage() . '</error>');
            return Cli::RETURN_FAILURE;
        }

        return Cli::RETURN_SUCCESS;
    }


    public function processProducts(OutputInterface $output): void
    {
        $storeId = 0;
        $batchSize = 500;

        $collection = $this->productCollectionFactory->create();
        $collection->addCategoriesFilter(['eq' => 59]);
        $collection->addAttributeToSelect('*'); 

        $collection->setPageSize($batchSize);
        $lastPageNumber = $collection->getLastPageNumber();

        $total = $collection->getSize();
        $output->writeln("<info>Found $total products in category 59.</info>");

        $updatedCount = 0;

        for ($currentPage = 1; $currentPage <= $lastPageNumber; $currentPage++) {
            $collection->setCurPage($currentPage);
            $collection->clear();

            foreach ($collection as $product) {
                $product->setStoreId($storeId);

                $wasModified = $this->specsManager->updateDescription($product);
                
                if ($wasModified) {
                    $this->productAction->updateAttributes(
                        [$product->getId()],
                        ['description' => $product->getData('description')],
                        $storeId
                    );
                    
                    $updatedCount++;
                    if ($updatedCount % 50 === 0) {
                        $output->writeln("Updated $updatedCount products...");
                    }
                }
            }
        }

        $output->writeln("<info>Done. Updated $updatedCount products out of $total.</info>");
    }
}
