<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 */

namespace Aitoc\PreOrders\Setup\Operations;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\SchemaSetupInterface;
use Aitoc\PreOrders\Api\Data\ProductAttributesInterface;
use Aitoc\PreOrders\Model\ResourceModel\ProductAttributes as ProductAttributesResourceModel;

/**
 * Class CreateProductAttributesTable
 */
class CreateProductAttributesTable
{
    /**
     * @param SchemaSetupInterface $setup
     * @throws \Zend_Db_Exception
     */
    public function execute(SchemaSetupInterface $setup)
    {
        $table = $setup->getConnection()->newTable(
            $setup->getTable(ProductAttributesResourceModel::TABLE_NAME)
        )->addColumn(
            ProductAttributesInterface::ENTITY_ID,
            Table::TYPE_SMALLINT,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity ID'
        )->addColumn(
            ProductAttributesInterface::PRODUCT_ID,
            Table::TYPE_INTEGER,
            null,
            ['unsigned' => true, 'nullable' => false],
            'Product ID'
        )->addColumn(
            ProductAttributesInterface::ATTRIBUTE_CODE,
            Table::TYPE_TEXT,
            255,
            ['default' => '', 'nullable' => false],
            'Extensible Attribute Code'
        )->addColumn(
            ProductAttributesInterface::VALUE,
            Table::TYPE_TEXT,
            255,
            ['default' => '', 'nullable' => false],
            'Value'
        )->addColumn(
            ProductAttributesInterface::STORE_ID,
            Table::TYPE_INTEGER,
            null,
            [],
            'Store Id'
        )->addForeignKey(
            $setup->getFkName(
                ProductAttributesResourceModel::TABLE_NAME,
                ProductAttributesInterface::PRODUCT_ID,
                'catalog_product_entity',
                'entity_id'
            ),
            ProductAttributesInterface::PRODUCT_ID,
            $setup->getTable('catalog_product_entity'),
            'entity_id',
            Table::ACTION_CASCADE
        )->setComment(
            'Additional Product Attributes'
        );

        $setup->getConnection()->createTable($table);
    }
}