<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateReports extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('reports');
        $table->addColumn('category_id', 'integer', ['null' => false, 'signed' => false]);
        $table->addColumn('reporter_id', 'integer', ['null' => false, 'signed' => false]);

        $this->addIpBinaryColumn($table, 'ip_bin', ['null' => false]);
        $table->addColumn('ip_text', 'string', ['limit' => 45, 'null' => false]);

        $table->addColumn('weight_at_report', 'decimal', ['precision' => 5, 'scale' => 2, 'null' => false]);
        $table->addColumn('metadata_json', 'text', ['null' => true]);

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

        $table
            ->addIndex(['ip_bin', 'category_id', 'received_at'], ['name' => 'idx_reports_ip_cat_received'])
            ->addIndex(['ip_bin'], ['name' => 'idx_reports_ip_bin'])
            ->addIndex(['category_id'])
            ->addIndex(['reporter_id'])
            ->addIndex(['received_at'])
            ->addForeignKey(
                'category_id',
                'categories',
                'id',
                ['delete' => 'RESTRICT', 'update' => 'NO_ACTION', 'constraint' => 'fk_reports_category']
            )
            ->addForeignKey(
                'reporter_id',
                'reporters',
                'id',
                ['delete' => 'RESTRICT', 'update' => 'NO_ACTION', 'constraint' => 'fk_reports_reporter']
            )
            ->create();
    }
}
