<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * Control DB. Write-once audit trail for platform-operator actions
 * (suspending a hotel, changing a plan price, marking an invoice paid) —
 * the control-DB counterpart to a tenant's own `activity_log`. Never
 * shares rows with any tenant's log: admin_user_id points at admin_users,
 * not any tenant's users table.
 */
class CreateAdminActivityLogTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'admin_user_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'module' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
            ],
            'action' => [
                'type' => 'TEXT',
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('admin_user_id', 'admin_users', 'id', '', 'SET NULL');
        $this->forge->createTable('admin_activity_log');
    }

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