<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateIpScores extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('ip_scores', [
            'id' => false,
            'primary_key' => ['ip_bin', 'category_id'],
        ]);

        $this->addIpBinaryColumn($table, 'ip_bin', ['null' => false]);
        $table->addColumn('category_id', 'integer', ['null' => false, 'signed' => false]);
        $table->addColumn('ip_text', 'string', ['limit' => 45, 'null' => false]);
        $table->addColumn('score', 'decimal', ['precision' => 12, 'scale' => 4, 'null' => false, 'default' => '0.0000']);
        $table->addColumn('report_count_30d', 'integer', ['null' => false, 'default' => 0, 'signed' => false]);

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

        $table
            ->addIndex(['category_id'])
            ->addIndex(['score'])
            ->addForeignKey(
                'category_id',
                'categories',
                'id',
                ['delete' => 'CASCADE', 'update' => 'NO_ACTION', 'constraint' => 'fk_ip_scores_category']
            )
            ->create();
    }
}
