<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateConsumers extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('consumers');
        $table
            ->addColumn('name', 'string', ['limit' => 128, 'null' => false])
            ->addColumn('description', 'text', ['null' => true])
            ->addColumn('policy_id', 'integer', ['null' => false, 'signed' => false])
            ->addColumn('is_active', 'boolean', ['null' => false, 'default' => true])
            ->addColumn('created_by_user_id', 'integer', ['null' => true, 'signed' => false]);

        $this->addTimestampColumn($table, 'created_at');
        $this->addTimestampColumn($table, 'last_pulled_at', ['null' => true]);

        $table
            ->addIndex(['name'], ['unique' => true, 'name' => 'uniq_consumers_name'])
            ->addIndex(['policy_id'])
            ->addIndex(['created_by_user_id'])
            ->addForeignKey(
                'policy_id',
                'policies',
                'id',
                ['delete' => 'RESTRICT', 'update' => 'NO_ACTION', 'constraint' => 'fk_consumers_policy']
            )
            ->addForeignKey(
                'created_by_user_id',
                'users',
                'id',
                ['delete' => 'SET_NULL', 'update' => 'NO_ACTION', 'constraint' => 'fk_consumers_created_by']
            )
            ->create();
    }
}
