<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateJobRuns extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('job_runs');
        $table
            ->addColumn('job_name', 'string', ['limit' => 64, 'null' => false])
            ->addColumn('status', 'string', ['limit' => 32, 'null' => false])
            ->addColumn('items_processed', 'integer', ['null' => false, 'default' => 0])
            ->addColumn('error_message', 'text', ['null' => true])
            ->addColumn('triggered_by', 'string', ['limit' => 32, 'null' => false]);

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

        $table
            ->addIndex(['job_name', 'started_at'], ['name' => 'idx_job_runs_job_started'])
            ->addIndex(['status'])
            ->create();
    }
}
