<?php

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

/**
 * Audit + progress log for a tenant's storage-source switch (migrate or clean),
 * polled by the master-control UI while the queued job runs.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('storage_migrations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('tenant_id')->constrained('tenants')->cascadeOnDelete();
            $table->foreignId('from_source_id')->nullable()->constrained('storage_sources')->nullOnDelete();
            $table->foreignId('to_source_id')->constrained('storage_sources')->cascadeOnDelete();
            $table->string('mode', 20); // migrate|clean
            $table->boolean('backup')->default(false);
            $table->string('status', 20)->default('pending'); // pending|running|completed|failed
            $table->unsignedBigInteger('bytes_total')->default(0);
            $table->unsignedBigInteger('bytes_done')->default(0);
            $table->text('error')->nullable();
            $table->timestamps();
        });
    }

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