<?php

declare(strict_types=1);

use App\Infrastructure\Db\Migrations\BaseMigration;

final class CreateUsers extends BaseMigration
{
    public function change(): void
    {
        $table = $this->table('users');
        $table
            ->addColumn('subject', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('email', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('display_name', 'string', ['limit' => 255, 'null' => true])
            ->addColumn('role', 'string', ['limit' => 32, 'null' => false])
            ->addColumn('is_local', 'boolean', ['null' => false, 'default' => false]);

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

        // SQLite and MySQL both treat NULLs as distinct in unique indexes, so a
        // full unique index on `subject` works for either driver — local users
        // simply have NULL subjects.
        $table
            ->addIndex(['subject'], ['unique' => true, 'name' => 'uniq_users_subject'])
            ->addIndex(['email'])
            ->addIndex(['is_local']);

        $table->create();
    }
}
