<?php

namespace Sibs\Sibs\Setup\Migrations;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Sibs\Sibs\Model\SibsConstants;
use Sibs\Sibs\Setup\SibsUpgradeSchemaInterface;
use Sibs\Sibs\Utils\SibsLog;

class UpgradeSchema248 implements SibsUpgradeSchemaInterface
{
    private $upgradeVersion = '2.4.8';

    private $context;

    public function __construct(ModuleContextInterface $context)
    {
        $this->context = $context;

        SibsLog::setNameFile(SibsConstants::SIBS_LOG_INSTALL);
    }

    public function getUpgradeVersion()
    {
        return $this->upgradeVersion;
    }

    public function validateVersion()
    {
        return version_compare($this->context->getVersion(), $this->getUpgradeVersion(), '<');
    }

    public function upgrade(SchemaSetupInterface $setup)
    {
        if (! $this->validateVersion()) {
            SibsLog::warn('Will not update schema on ' . $this->context->getVersion() . ' to version ' . $this->getUpgradeVersion());

            return;
        }

        SibsLog::warn('Try to Update Schema to version ' . $this->getUpgradeVersion());

        $tableName  = $setup->getTable('sibs_webhook_calls');
        $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('notification_id', Table::TYPE_TEXT, Table::DEFAULT_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('url', Table::TYPE_TEXT, Table::DEFAULT_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('headers', Table::TYPE_TEXT, Table::MAX_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('payload', Table::TYPE_TEXT, Table::MAX_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => false
                ])
                ->addColumn('decoded_payload', Table::TYPE_TEXT, Table::MAX_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('exception', Table::TYPE_TEXT, Table::MAX_TEXT_SIZE, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('arrived', Table::TYPE_INTEGER, 11, [
                    Table::OPTION_NULLABLE => true,
                    Table::OPTION_DEFAULT => 1
                ])
                ->addColumn('processed_qty', Table::TYPE_INTEGER, 11, [
                    Table::OPTION_NULLABLE => true,
                    Table::OPTION_DEFAULT => 0
                ])
                ->addColumn('processed_at', Table::TYPE_DATETIME, null, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('created_at', Table::TYPE_DATETIME, null, [
                    Table::OPTION_NULLABLE => true
                ])
                ->addColumn('updated_at', Table::TYPE_DATETIME, null, [
                    Table::OPTION_NULLABLE => true
                ]);
            $connection->createTable($table);
        }

        SibsLog::warn('Updated Schema version ' . $this->getUpgradeVersion());
    }
}
