<?php
/**
 * For the full copyright and license information, please view the
 * docs/licenses/LICENSE.txt file that was distributed with this source code.
 */
declare(strict_types=1);

namespace PrestaShop\PrestaShop\Adapter\Supplier\CommandHandler;

use Address;
use PrestaShop\PrestaShop\Adapter\Supplier\AbstractSupplierHandler;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Supplier\Command\EditSupplierCommand;
use PrestaShop\PrestaShop\Core\Domain\Supplier\CommandHandler\EditSupplierHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Supplier\Exception\SupplierException;
use PrestaShopException;
use Supplier;

/**
 * Handles command which edits supplier using legacy object model
 */
#[AsCommandHandler]
final class EditSupplierHandler extends AbstractSupplierHandler implements EditSupplierHandlerInterface
{
    /**
     * {@inheritdoc}
     *
     * @throws SupplierException
     */
    public function handle(EditSupplierCommand $command)
    {
        $supplierId = $command->getSupplierId();
        $supplier = $this->getSupplier($supplierId);
        $address = $this->getSupplierAddress($supplierId);

        $this->populateSupplierWithData($supplier, $command);
        $this->populateAddressWithData($address, $command);

        try {
            $this->validateFields($supplier, $address);

            if (false === $supplier->update()) {
                throw new SupplierException(sprintf('Cannot update supplier with id "%s"', $supplier->id));
            }
            if (false === $address->update()) {
                throw new SupplierException(sprintf('Cannot update supplier address with id "%s"', $address->id));
            }
            if (null !== $command->getAssociatedShops()) {
                $this->associateWithShops($supplier, $command->getAssociatedShops());
            }
        } catch (PrestaShopException) {
            throw new SupplierException(sprintf('Cannot update supplier with id "%s"', $supplier->id));
        }
    }

    /**
     * Populates Supplier object with given data
     *
     * @param Supplier $supplier
     * @param EditSupplierCommand $command
     */
    private function populateSupplierWithData(Supplier $supplier, EditSupplierCommand $command)
    {
        if (null !== $command->getName()) {
            $supplier->name = $command->getName();
        }
        if (null !== $command->getLocalizedDescriptions()) {
            $supplier->description = $command->getLocalizedDescriptions();
        }
        if (null !== $command->getLocalizedMetaDescriptions()) {
            $supplier->meta_description = $command->getLocalizedMetaDescriptions();
        }
        if (null !== $command->getLocalizedMetaTitles()) {
            $supplier->meta_title = $command->getLocalizedMetaTitles();
        }
        if (null !== $command->isEnabled()) {
            $supplier->active = $command->isEnabled();
        }
        $supplier->date_upd = date('Y-m-d H:i:s');
    }

    /**
     * Populates Supplier address with given data
     *
     * @param Address $address
     * @param EditSupplierCommand $command
     */
    private function populateAddressWithData(Address $address, EditSupplierCommand $command)
    {
        if (null !== $command->getAddress()) {
            $address->address1 = $command->getAddress();
        }
        if (null !== $command->getAddress2()) {
            $address->address2 = $command->getAddress2();
        }
        if (null !== $command->getPostCode()) {
            $address->postcode = $command->getPostCode();
        }
        if (null !== $command->getPhone()) {
            $address->phone = $command->getPhone();
        }
        if (null !== $command->getMobilePhone()) {
            $address->phone_mobile = $command->getMobilePhone();
        }
        if (null !== $command->getCity()) {
            $address->city = $command->getCity();
        }
        if (null !== $command->getCountryId()) {
            $address->id_country = $command->getCountryId();
        }
        if (null !== $command->getStateId()) {
            $address->id_state = $command->getStateId();
        }
        if (null !== $command->getDni()) {
            $address->dni = $command->getDni();
        }
    }
}
