<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreatePolicyCategoryThresholds extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('policy_category_thresholds', [
            'id' => false,
            'primary_key' => ['policy_id', 'category_id'],
        ]);
        $table
            ->addColumn('policy_id', 'integer', ['null' => false, 'signed' => false])
            ->addColumn('category_id', 'integer', ['null' => false, 'signed' => false])
            ->addColumn('threshold', 'decimal', ['precision' => 10, 'scale' => 4, 'null' => false])
            ->addForeignKey(
                'policy_id',
                'policies',
                'id',
                ['delete' => 'CASCADE', 'update' => 'NO_ACTION', 'constraint' => 'fk_pct_policy']
            )
            ->addForeignKey(
                'category_id',
                'categories',
                'id',
                ['delete' => 'RESTRICT', 'update' => 'NO_ACTION', 'constraint' => 'fk_pct_category']
            )
            ->addIndex(['category_id'])
            ->create();
    }
}
