<?php

namespace Sibs\Sibs\Setup;

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

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $this->createPaymentRecurringTables($setup);
        $this->updateTableOrders($setup);

        $setup->endSetup();
    }

    protected function createPaymentRecurringTables($setup)
    {
        $tableName  = $setup->getTable('sibs_payment_cards_tables');
        $connection = $setup->getConnection();

        if ($connection->isTableExists($tableName) != true) {
            $table = $connection
                ->newTable($tableName)
                ->addColumn('id', Table::TYPE_INTEGER, 10, [
                    Table::OPTION_IDENTITY => true,
                    Table::OPTION_UNSIGNED => true,
                    Table::OPTION_NULLABLE => false,
                    Table::OPTION_PRIMARY => true,
                    'auto_increment' => true,
                ])
                ->addColumn('customer_id', Table::TYPE_INTEGER, 10, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('bin', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('brand', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('holder', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('email', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('last4digits', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('expiry_month', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('expiry_year', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('payment_default', Table::TYPE_TEXT, 255, [
                    Table::OPTION_NULLABLE => false
                ]);
            $connection->createTable($table);
        }
    }

    protected function updateTableOrders($setup)
    {
        $salesOrderTable = $setup->getTable('sales_order');
        $connection      = $setup->getConnection();

        // For all sibs orders
        if ($connection->tableColumnExists($salesOrderTable, 'sibs_transaction_id') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_transaction_id', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Transaction ID',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_payment_method') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_payment_method', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Payment Method',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_ref_id') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_ref_id', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Reference ID',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_payment_code') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_payment_code', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Payment Code',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_amount') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_amount', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Amount',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_currency') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_currency', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Currency',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_payment_type') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_payment_type', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Payment Type',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_is_capture') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_is_capture', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Capture State',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_refunded_id') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_refunded_id', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Refunded ID for Async Refund',
            ]);
        }

        if ($connection->tableColumnExists($salesOrderTable, 'sibs_refunded_status') === false) {
            $connection->addColumn($salesOrderTable, 'sibs_refunded_status', [
                'type'    => Table::TYPE_TEXT,
                'length'  => 255,
                'comment' => 'Refunded status for Async Refund',
            ]);
        }
    }
}
