<?php

namespace Tenant\Database\Migrations;

use CodeIgniter\Database\Migration;

/**
 * `module` is included from day one (bookings, checkin, folio, guest, ...)
 * even though Phase 1's UI for this log is just a plain list — Phase 4
 * expands it into a filterable-by-module audit trail, and rows written
 * without a module value couldn't be back-filled reliably.
 *
 * `created_at` is this table's "timestamp" column from the spec; named to
 * match the rest of the schema rather than using the SQL type name TIMESTAMP
 * as a literal column name.
 */
class CreateActivityLogTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'user_id' => [
                'type'     => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'null'     => true,
            ],
            'module' => [
                'type'       => 'VARCHAR',
                'constraint' => 50,
                'comment'    => 'e.g. bookings, checkin, folio, guest',
            ],
            'action' => [
                'type' => 'TEXT',
            ],
            'created_at' => [
                'type' => 'DATETIME',
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('user_id');
        $this->forge->addKey('module');
        $this->forge->addKey('created_at');
        $this->forge->addForeignKey('user_id', 'users', 'id', '', 'SET NULL');
        $this->forge->createTable('activity_log');
    }

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