<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateSystemUpdates extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'applied_by_user_id' => ['type' => 'BIGINT', 'unsigned' => true],
            'original_filename'  => ['type' => 'VARCHAR', 'constraint' => 255],
            'backup_path'        => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],
            'files_changed'      => ['type' => 'INT', 'unsigned' => true, 'default' => 0],
            'status'             => ['type' => 'ENUM', 'constraint' => ['applied', 'rolled_back', 'failed'], 'default' => 'applied'],
            'notes'              => ['type' => 'TEXT', 'null' => true],
            'created_at'         => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addForeignKey('applied_by_user_id', 'users', 'id', false, 'RESTRICT');
        $this->forge->createTable('system_updates', true, ['ENGINE' => 'InnoDB']);
    }

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