<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAuditLogs extends Migration
{
    private array $logTables = [
        'logpatient' => 'LogPatientID',
        'logorder'   => 'LogOrderID',
        'logmaster'  => 'LogMasterID',
        'logsystem'  => 'LogSystemID',
    ];

    public function up(): void
    {
        foreach ($this->logTables as $table => $pk) {
            $this->createLogTable($table, $pk);
        }
    }

    public function down(): void
    {
        foreach (array_reverse($this->logTables) as $table => $pk) {
            $this->forge->dropTable($table, true);
        }
    }

    private function createLogTable(string $table, string $primaryKey): void
    {
        $fields = [
            $primaryKey => [
                'type'           => 'BIGINT',
                'constraint'     => 20,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'TblName' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
            ],
            'RecID' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
            ],
            'FldName' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'FldValuePrev' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'FldValueNew' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'UserID' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
            ],
            'SiteID' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
            ],
            'DIDType' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
            ],
            'DID' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'MachineID' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'SessionID' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
            ],
            'AppID' => [
                'type'       => 'VARCHAR',
                'constraint' => 64,
            ],
            'ProcessID' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'WebPageID' => [
                'type'       => 'VARCHAR',
                'constraint' => 128,
                'null'       => true,
            ],
            'EventID' => [
                'type'       => 'VARCHAR',
                'constraint' => 80,
            ],
            'ActivityID' => [
                'type'       => 'VARCHAR',
                'constraint' => 24,
            ],
            'Reason' => [
                'type'       => 'VARCHAR',
                'constraint' => 512,
                'null'       => true,
            ],
            'LogDate' => [
                'type'       => 'DATETIME',
                'constraint' => 3,
            ],
            'Context' => [
                'type' => 'JSON',
            ],
            'IpAddress' => [
                'type'       => 'VARCHAR',
                'constraint' => 45,
                'null'       => true,
            ],
        ];

        $this->forge->addField($fields);
        $this->forge->addKey($primaryKey, true);
        $this->forge->addKey(['LogDate'], false, false, "idx_{$table}_logdate");
        $this->forge->addKey(['RecID', 'LogDate'], false, false, "idx_{$table}_recid_logdate");
        $this->forge->addKey(['UserID', 'LogDate'], false, false, "idx_{$table}_userid_logdate");
        $this->forge->addKey(['EventID', 'LogDate'], false, false, "idx_{$table}_eventid_logdate");
        $this->forge->addKey(['SiteID', 'LogDate'], false, false, "idx_{$table}_site_logdate");
        $this->forge->createTable($table, true);
    }
}
