<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('upload_sessions', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('bucket_id')->constrained('file_buckets')->cascadeOnDelete();
            $table->string('original_name');
            $table->unsignedBigInteger('total_size');
            $table->unsignedInteger('chunk_size');
            $table->unsignedInteger('total_chunks');
            $table->json('uploaded_chunks')->nullable()->comment('Array of uploaded chunk indices');
            $table->string('checksum', 64)->nullable()->comment('Final file checksum');
            $table->enum('status', ['pending', 'uploading', 'assembling', 'completed', 'failed', 'expired'])->default('pending');
            $table->string('mime_type')->nullable()->comment('Validated after first chunk');
            $table->json('metadata')->nullable();
            $table->timestamp('expires_at');
            $table->timestamps();

            $table->index('uuid');
            $table->index('user_id');
            $table->index('bucket_id');
            $table->index('status');
            $table->index('expires_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('upload_sessions');
    }
};
