<?php

declare(strict_types=1);

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAdminActivityLogs extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'BIGINT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'admin_user' => [
                'type'       => 'VARCHAR',
                'constraint' => 191,
                'default'    => '',
            ],
            'action' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'default'    => '',
            ],
            'endpoint' => [
                'type'       => 'VARCHAR',
                'constraint' => 512,
                'default'    => '',
            ],
            'payload' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'ip_address' => [
                'type'       => 'VARCHAR',
                'constraint' => 45,
                'default'    => '',
            ],
            'user_agent' => [
                'type'       => 'VARCHAR',
                'constraint' => 512,
                'default'    => '',
            ],
            'auth_source' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
            ],
            'roles_json' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'outcome' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'default'    => 'success',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => false,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('created_at');
        $this->forge->addKey(['action', 'created_at']);
        $this->forge->createTable('admin_activity_logs', true, [
            'ENGINE' => 'InnoDB',
        ]);
    }

    public function down(): void
    {
        $this->forge->dropTable('admin_activity_logs', true);
    }
}
