<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('module_update_logs', function (Blueprint $table) {
            $table->id();
            $table->string('module_slug', 80)->index();
            $table->string('from_version', 30)->nullable();
            $table->string('to_version',   30);
            $table->enum('status', ['pending', 'downloading', 'verifying', 'applying', 'completed', 'failed', 'rolled_back'])
                  ->default('pending');
            $table->string('error_message')->nullable();
            $table->text('changelog')->nullable();
            $table->unsignedBigInteger('initiated_by')->nullable(); // admin user id
            $table->timestamp('finished_at')->nullable();
            $table->timestamps();

            $table->index(['module_slug', 'created_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('module_update_logs');
    }
};
