<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('media', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->foreignId('academy_id')->constrained('academies');
            $table->string('collection', 50);
            $table->string('original_filename');
            $table->string('disk_path');
            $table->string('mime_type', 50);
            $table->unsignedInteger('width')->default(0);
            $table->unsignedInteger('height')->default(0);
            $table->unsignedInteger('file_size')->default(0);
            $table->string('alt_text')->nullable();
            $table->string('alt_text_ar')->nullable();
            $table->nullableMorphs('mediable');
            $table->unsignedSmallInteger('sort_order')->default(0);
            $table->timestamps();
            $table->softDeletes();

            $table->index(['academy_id', 'collection']);
        });

        DB::statement("ALTER TABLE media ADD CONSTRAINT media_collection_check CHECK (collection IN (
            'academy_logo', 'academy_cover', 'activity_photo', 'branch_photo',
            'gallery', 'team_photo', 'testimonial_avatar', 'partner_logo',
            'news_image', 'section_image', 'general'
        ))");
    }

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