<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('product_images')) {
            return; // Table already exists
        }
        Schema::create('product_images', function (Blueprint $table) {
            $table->id();
            $table->foreignId('product_id')->constrained()->onDelete('cascade');
            $table->string('path');
            $table->string('filename');
            $table->integer('position')->default(0); // Pour l'ordre d'affichage
            $table->boolean('is_primary')->default(false); // Image principale
            $table->string('alt_text')->nullable(); // Texte alternatif pour SEO
            $table->bigInteger('size')->nullable(); // Taille en bytes
            $table->timestamps();

            $table->index(['product_id', 'position']);
            $table->index(['product_id', 'is_primary']);
        });
    }

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