<?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
    {
        Schema::table('artworks', function (Blueprint $table) {
            // Remove unique constraint on hash and add a non-unique index
            try {
                $table->dropUnique(['hash']);
            } catch (\Throwable $e) {
                // Ignore if it doesn't exist
            }

            // Add regular index for faster lookups. Wrap in try/catch since
            // checking existing indexes requires DBAL which may not be installed.
            try {
                if (Schema::hasColumn('artworks', 'hash')) {
                    $table->index('hash', 'artworks_hash_index');
                }
            } catch (\Throwable $e) {
                // Ignore index creation errors (likely already exists)
            }
        });
    }

    public function down(): void
    {
        Schema::table('artworks', function (Blueprint $table) {
            // Restore unique constraint (if desired)
            try {
                $table->dropIndex('artworks_hash_index');
            } catch (\Throwable $e) {
            }

            try {
                $table->unique('hash', 'artworks_hash_unique');
            } catch (\Throwable $e) {
            }
        });
    }
};
