<?php

declare(strict_types=1);

namespace Gls\GlsPoland\PrestaShop\Installer\Migration;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;

final class Version1_1_7 extends AbstractMigration
{
    private const ORDER_PARCEL_SHOP_TABLE = 'gls_poland_order_parcel_shop';

    public function getVersion(): string
    {
        return '1.1.7';
    }

    public function up(Schema $schema): void
    {
        $this->configureOrderParcelShopTable($this->createTable($schema, self::ORDER_PARCEL_SHOP_TABLE));
    }

    private function configureOrderParcelShopTable(Table $table): void
    {
        $this->addColumn($table, 'id_order', 'integer')->setLength(10)->setUnsigned(true);
        $this->addColumn($table, 'parcel_shop_id', 'string')->setLength(32);
        $this->addColumn($table, 'name', 'string')->setLength(255);
        $this->addColumn($table, 'street', 'string')->setLength(255)->setNotnull(false);
        $this->addColumn($table, 'postal_code', 'string')->setLength(32)->setNotnull(false);
        $this->addColumn($table, 'city', 'string')->setLength(128)->setNotnull(false);
        $this->setPrimaryKey($table, ['id_order']);

        if (0 === count($table->getForeignKeys())) {
            $table->addForeignKeyConstraint($this->prefixTableName('orders'), ['id_order'], ['id_order'], [
                'onDelete' => 'CASCADE',
            ]);
        }
    }
}
