<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateSettingsAndLogs extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'           => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'group'        => ['type' => 'VARCHAR', 'constraint' => 50],
            'key'          => ['type' => 'VARCHAR', 'constraint' => 100],
            'value'        => ['type' => 'TEXT', 'null' => true],
            'is_encrypted' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'updated_at'   => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['group', 'key']);
        $this->forge->createTable('settings', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'         => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id'    => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'agency_id'  => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'role'       => ['type' => 'VARCHAR', 'constraint' => 50, 'null' => true],
            'action'     => ['type' => 'VARCHAR', 'constraint' => 100],
            'ip_address' => ['type' => 'VARCHAR', 'constraint' => 45, 'null' => true],
            'device'     => ['type' => 'VARCHAR', 'constraint' => 150, 'null' => true],
            'browser'    => ['type' => 'VARCHAR', 'constraint' => 150, 'null' => true],
            'metadata'   => ['type' => 'JSON', 'null' => true],
            'created_at' => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['user_id', 'created_at']);
        $this->forge->addKey('action');
        $this->forge->createTable('activity_logs', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'         => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id'    => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'module'     => ['type' => 'VARCHAR', 'constraint' => 100],
            'action'     => ['type' => 'VARCHAR', 'constraint' => 100],
            'record_id'  => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'old_value'  => ['type' => 'JSON', 'null' => true],
            'new_value'  => ['type' => 'JSON', 'null' => true],
            'created_at' => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['module', 'record_id']);
        $this->forge->createTable('audit_logs', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'           => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'identifier'   => ['type' => 'VARCHAR', 'constraint' => 150],
            'route'        => ['type' => 'VARCHAR', 'constraint' => 150],
            'hit_count'    => ['type' => 'INT', 'unsigned' => true, 'default' => 1],
            'window_start' => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['identifier', 'route']);
        $this->forge->createTable('rate_limit_hits', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('rate_limit_hits', true);
        $this->forge->dropTable('audit_logs', true);
        $this->forge->dropTable('activity_logs', true);
        $this->forge->dropTable('settings', true);
    }
}
