<?php
/**
 * Install Schema for PortalPeople ShippingFields Module
 */

namespace PortalPeople\ShippingFields\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class InstallSchema implements InstallSchemaInterface
{
    /**
     * Install schema for shipping fields
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        // Add shipping_date and shipping_message to quote table
        $quoteTable = $installer->getTable('quote');
        if ($installer->getConnection()->isTableExists($quoteTable)) {
            if (!$installer->getConnection()->tableColumnExists($quoteTable, 'shipping_date')) {
                $installer->getConnection()->addColumn(
                    $quoteTable,
                    'shipping_date',
                    [
                        'type' => Table::TYPE_DATE,
                        'nullable' => true,
                        'comment' => 'Shipping Date'
                    ]
                );
            }

            if (!$installer->getConnection()->tableColumnExists($quoteTable, 'shipping_message')) {
                $installer->getConnection()->addColumn(
                    $quoteTable,
                    'shipping_message',
                    [
                        'type' => Table::TYPE_TEXT,
                        'length' => 255,
                        'nullable' => true,
                        'comment' => 'Shipping Message'
                    ]
                );
            }
        }

        // Add shipping_date and shipping_message to sales_order table
        $orderTable = $installer->getTable('sales_order');
        if ($installer->getConnection()->isTableExists($orderTable)) {
            if (!$installer->getConnection()->tableColumnExists($orderTable, 'shipping_date')) {
                $installer->getConnection()->addColumn(
                    $orderTable,
                    'shipping_date',
                    [
                        'type' => Table::TYPE_DATE,
                        'nullable' => true,
                        'comment' => 'Shipping Date'
                    ]
                );
            }

            if (!$installer->getConnection()->tableColumnExists($orderTable, 'shipping_message')) {
                $installer->getConnection()->addColumn(
                    $orderTable,
                    'shipping_message',
                    [
                        'type' => Table::TYPE_TEXT,
                        'length' => 255,
                        'nullable' => true,
                        'comment' => 'Shipping Message'
                    ]
                );
            }
        }

        $installer->endSetup();
    }
}
