<?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\PreOrderSubscribersInterface;
use Aitoc\PreOrders\Model\ResourceModel\PreOrderSubscribers as PreOrderSubscribersResourceModel;

/**
 * Class CreatePreOrderSubscribersTable
 */
class CreatePreOrderSubscribersTable
{
    /**
     * @param SchemaSetupInterface $setup
     * @throws \Zend_Db_Exception
     */
    public function execute(SchemaSetupInterface $setup)
    {
        $table = $setup->getConnection()->newTable(
            $setup->getTable(PreOrderSubscribersResourceModel::TABLE_NAME)
        )->addColumn(
            PreOrderSubscribersInterface::ENTITY_ID,
            Table::TYPE_SMALLINT,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'Entity ID'
        )->addColumn(
            PreOrderSubscribersInterface::PRODUCT_ID,
            Table::TYPE_INTEGER,
            null,
            ['unsigned' => true, 'nullable' => false],
            'Product ID'
        )->addColumn(
            PreOrderSubscribersInterface::CUSTOMER_NAME,
            Table::TYPE_TEXT,
            255,
            ['default' => '', 'nullable' => false],
            'Customer Name'
        )->addColumn(
            PreOrderSubscribersInterface::CUSTOMER_EMAIL,
            Table::TYPE_TEXT,
            255,
            ['default' => '', 'nullable' => false],
            'Customer Email'
        )->addColumn(
            PreOrderSubscribersInterface::CREATED_AT,
            Table::TYPE_TIMESTAMP,
            null,
            ['default' => Table::TIMESTAMP_INIT, 'nullable' => false],
            'Creation Timestamp'
        )->addColumn(
            PreOrderSubscribersInterface::SENDING_STATUS,
            Table::TYPE_SMALLINT,
            null,
            ['default' => 0, 'nullable' => false],
            'Subscription Status'
        )->addColumn(
            PreOrderSubscribersInterface::MESSAGE,
            Table::TYPE_TEXT,
            255,
            ['default' => '', 'nullable' => false],
            'Note or Error Message'
        )->addForeignKey(
            $setup->getFkName(
                PreOrderSubscribersResourceModel::TABLE_NAME,
                PreOrderSubscribersInterface::PRODUCT_ID,
                'catalog_product_entity',
                'entity_id'
            ),
            PreOrderSubscribersInterface::PRODUCT_ID,
            $setup->getTable('catalog_product_entity'),
            'entity_id',
            Table::ACTION_CASCADE
        )->setComment(
            'Pre-Order Settings for Order Items'
        );

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