<?php

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

/**
 * Per-file storage registry. One row per (disk, path) — written by
 * MediaStorage::put on every upload, dropped by MediaStorage::delete.
 *
 * The `disk` column is intentionally a free-form string (not enum) so adding
 * a sixth provider is a code change, not an ALTER TABLE
 * `owner_type`/`owner_id` form a polymorphic pointer at the domain row that
 * "owns" the file (a Project, ThemeVersion, branding singleton, etc.) so a
 * delete-by-owner sweep can find every file we wrote on its behalf without
 * the domain row needing to track a list. `purpose` is a free-form bucket so
 * an admin viewer can group files by intent ('screen-image' / 'logo' /
 * 'export-tile' / etc.) without joining N tables.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('media_files', function (Blueprint $table): void {
            $table->id();
            $table->string('disk', 32);
            $table->string('path', 512);
            $table->string('original_name', 255)->nullable();
            $table->string('mime_type', 191)->nullable();
            $table->unsignedBigInteger('size_bytes')->nullable();
            $table->string('owner_type', 64)->nullable();
            $table->unsignedBigInteger('owner_id')->nullable();
            $table->string('purpose', 32)->nullable();
            $table->timestamps();

            $table->unique(['disk', 'path']);
            $table->index(['owner_type', 'owner_id']);
            $table->index(['disk']);
        });
    }

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