<?php

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

class CreateArtworkFeaturesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('artwork_features', function (Blueprint $table) {
            // Production-ready settings
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

            $table->bigIncrements('id');

            $table->unsignedBigInteger('artwork_id');

            $table->dateTime('featured_at')->nullable(false);
            $table->dateTime('expires_at')->nullable();

            $table->unsignedSmallInteger('priority')->default(100);

            $table->string('label', 100)->nullable();
            $table->text('note')->nullable();

            $table->boolean('is_active')->default(true);

            $table->unsignedBigInteger('created_by')->nullable();

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

            // Indexes
            $table->index(['is_active', 'featured_at', 'expires_at'], 'idx_features_active');
            $table->index(['is_active', 'priority', 'featured_at'], 'idx_features_priority');
            $table->index('artwork_id', 'idx_features_artwork');
            $table->unique(['artwork_id', 'is_active'], 'uq_features_artwork_active');

            // Foreign keys
            $table->foreign('artwork_id', 'fk_features_artwork')
                ->references('id')->on('artworks')
                ->onDelete('cascade');

            $table->foreign('created_by', 'fk_features_user')
                ->references('id')->on('users')
                ->onDelete('set null');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('artwork_features', function (Blueprint $table) {
            $table->dropForeign('fk_features_artwork');
            $table->dropForeign('fk_features_user');
        });

        Schema::dropIfExists('artwork_features');
    }
}
