<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateAllowlist extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('allowlist');
        $table->addColumn('kind', 'string', ['limit' => 16, 'null' => false]);

        $this->addIpBinaryColumn($table, 'ip_bin', ['null' => true]);
        $this->addIpBinaryColumn($table, 'network_bin', ['null' => true]);
        $table->addColumn('prefix_length', 'smallinteger', ['null' => true, 'signed' => false]);

        $table->addColumn('reason', 'text', ['null' => true]);
        $table->addColumn('created_by_user_id', 'integer', ['null' => true, 'signed' => false]);

        $this->addTimestampColumn($table, 'created_at');

        $table
            ->addIndex(['ip_bin'], ['name' => 'idx_allowlist_ip_bin'])
            ->addIndex(['network_bin'], ['name' => 'idx_allowlist_network_bin'])
            ->addIndex(['kind'])
            ->addIndex(['created_by_user_id'])
            ->addForeignKey(
                'created_by_user_id',
                'users',
                'id',
                ['delete' => 'SET_NULL', 'update' => 'NO_ACTION', 'constraint' => 'fk_allowlist_created_by']
            )
            ->create();
    }
}
