<?php

declare(strict_types=1);

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('enhance_jobs', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained()->cascadeOnDelete();
            $table->foreignId('artwork_id')->nullable()->constrained('artworks')->nullOnDelete();

            $table->string('status', 32)->default('pending');
            $table->string('engine', 64)->default('stub');
            $table->string('mode', 32)->default('standard');
            $table->unsignedTinyInteger('scale')->default(2);

            $table->string('source_disk', 64)->nullable();
            $table->string('source_path')->nullable();
            $table->string('source_hash', 128)->nullable();

            $table->unsignedInteger('input_width')->nullable();
            $table->unsignedInteger('input_height')->nullable();
            $table->unsignedBigInteger('input_filesize')->nullable();
            $table->string('input_mime', 128)->nullable();

            $table->string('output_disk', 64)->nullable();
            $table->string('output_path')->nullable();
            $table->string('output_hash', 128)->nullable();

            $table->unsignedInteger('output_width')->nullable();
            $table->unsignedInteger('output_height')->nullable();
            $table->unsignedBigInteger('output_filesize')->nullable();
            $table->string('output_mime', 128)->nullable();

            $table->string('preview_disk', 64)->nullable();
            $table->string('preview_path')->nullable();

            $table->unsignedInteger('processing_seconds')->nullable();
            $table->text('error_message')->nullable();
            $table->json('metadata')->nullable();

            $table->timestamp('queued_at')->nullable();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('finished_at')->nullable();
            $table->timestamp('expires_at')->nullable();

            $table->timestamps();
            $table->softDeletes();

            $table->index(['user_id', 'status']);
            $table->index(['status', 'created_at']);
            $table->index(['artwork_id']);
        });
    }

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