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

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Setup\Operations;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\SchemaSetupInterface;
use Aitoc\PreOrders\Api\Data\OrderItemPreOrderSettingsInterface;
use Aitoc\PreOrders\Model\ResourceModel\OrderItemPreOrderSettings as OrderItemPreOrderSettingsResourceModel;

/**
 * Class CreateOrderItemPreOrderSettingsTable
 */
class CreateOrderItemPreOrderSettingsTable
{
    /**
     * @param SchemaSetupInterface $setup
     * @throws \Zend_Db_Exception
     */
    public function execute(SchemaSetupInterface $setup)
    {
        $table = $setup->getConnection()->newTable(
            $setup->getTable(OrderItemPreOrderSettingsResourceModel::TABLE_NAME)
        )->addColumn(
            OrderItemPreOrderSettingsInterface::ENTITY_ID,
            Table::TYPE_SMALLINT,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity ID'
        )->addColumn(
            OrderItemPreOrderSettingsInterface::ORDER_ITEM_ID,
            Table::TYPE_INTEGER,
            null,
            ['unsigned' => true, 'nullable' => false],
            'Order Item ID'
        )->addColumn(
            OrderItemPreOrderSettingsInterface::IS_PRE_ORDER,
            Table::TYPE_SMALLINT,
            1,
            ['default' => 0, 'nullable' => false],
            'Pre Order Status'
        )->addColumn(
            OrderItemPreOrderSettingsInterface::IS_NOTIFIED,
            Table::TYPE_SMALLINT,
            1,
            ['default' => 0, 'nullable' => false],
            'Notification Status'
        )->addForeignKey(
            $setup->getFkName(
                OrderItemPreOrderSettingsResourceModel::TABLE_NAME,
                OrderItemPreOrderSettingsInterface::ORDER_ITEM_ID,
                'sales_order_item',
                'item_id'
            ),
            OrderItemPreOrderSettingsInterface::ORDER_ITEM_ID,
            $setup->getTable('sales_order_item'),
            'item_id',
            Table::ACTION_CASCADE
        )->setComment(
            'Pre-Order Settings for Order Items'
        );

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