<?php
/**
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Setup\Patch\Data;

use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
 * Patch is mechanism, that allows to do atomic upgrade data changes
 */
class CopyDataPatch implements DataPatchInterface
{
    /**
     * @var ModuleDataSetupInterface $moduleDataSetup
     */
    private $moduleDataSetup;

    /**
     *
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     * @param \Magetop\Quotesystem\Model\ResourceModel\Quotes\CollectionFactory $quoteCollection
     * @param \Magetop\Quotesystem\Model\QuoteDetailsFactory $quoteDetails
     * @param \Magetop\Quotesystem\Model\Quotes $quotes
     * @param \Psr\Log\LoggerInterface $logger
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        \Magetop\Quotesystem\Model\ResourceModel\Quotes\CollectionFactory $quoteCollection,
        \Magetop\Quotesystem\Model\QuoteDetailsFactory $quoteDetails,
        \Magetop\Quotesystem\Model\Quotes $quotes,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->quoteCollection = $quoteCollection;
        $this->quoteDetails = $quoteDetails;
        $this->quotes = $quotes;
        $this->logger = $logger;
    }

    /**
     * Do Upgrade
     *
     * @return void
     */
    public function apply()
    {
        $quoteCollection = $this->quoteCollection->create();
        if ($quoteCollection->getSize()) {
            foreach ($quoteCollection as $quoteData) {
                $this->moduleDataSetup->getConnection()->startSetup();
                $connection = $this->moduleDataSetup->getConnection();
                $quoteDetails = $this->quoteDetails->create();
                try {
                    $data = [
                        'entity_id' => $quoteData->getId(),
                        'customer_id' => $quoteData->getCustomerId(),
                        'created_at' => $quoteData->getCreatedAt(),
                        'quote_generate' => 1,
                    ];

                    $connection->insert($this->moduleDataSetup->getTable('mt_quote_details'), $data);
                    $table = 'mt_quotes';
                    $idField = 'entity_id';
                    $rowId = $quoteData->getId();
                    $field = 'quote_id';
                    $value = $quoteData->getId();
                    $this->moduleDataSetup->updateTableRow($table, $idField, $rowId, $field, $value);
                    $this->moduleDataSetup->getConnection()->endSetup();
                } catch (\Exception $e) {
                    $this->logger->info('Save Quote Data Error '.$e->getMessage());
                }
            }
        }
    }

    /**
     * Get Aliases
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * Get Dependencies
     */
    public static function getDependencies()
    {
        return [

        ];
    }
}
