<?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('advertisements', function (Blueprint $table) {
            foreach ([
                'large_mobile_banner_id',
                'medium_rectangle_id',
                'large_leaderboard_id',
                'super_banner_id',
                'large_banner_id',
                'slider_banner_id',
            ] as $column) {
                $table->dropForeign([$column]);
                $table->unsignedBigInteger($column)->nullable()->change();
                $table->foreign($column)
                    ->references('id')
                    ->on('media_items')
                    ->onDelete('SET NULL')
                    ->onUpdate('CASCADE');
            }
        });

        Schema::table('articles', function (Blueprint $table) {
            foreach ([
                'featured_image_id',
                'image_text_block_id',
                'image_magazine_id',
            ] as $column) {
                $table->dropForeign([$column]);
                $table->unsignedBigInteger($column)->nullable()->change();
                $table->foreign($column)
                    ->references('id')
                    ->on('media_items')
                    ->onDelete('SET NULL')
                    ->onUpdate('CASCADE');
            }
        });

        Schema::table('events', function (Blueprint $table) {
            $table->dropForeign(['featured_image_id']);
            $table->unsignedBigInteger('featured_image_id')->nullable()->change();
            $table->foreign('featured_image_id')
                ->references('id')
                ->on('media_items')
                ->onDelete('SET NULL')
                ->onUpdate('CASCADE');
        });
    }

    public function down(): void
    {
        Schema::table('advertisements', function (Blueprint $table) {
            foreach ([
                'large_mobile_banner_id',
                'medium_rectangle_id',
                'large_leaderboard_id',
                'super_banner_id',
                'large_banner_id',
                'slider_banner_id',
            ] as $column) {
                $table->dropForeign([$column]);
                $table->foreign($column)
                    ->references('id')
                    ->on('media_items')
                    ->onDelete('CASCADE')
                    ->onUpdate('CASCADE');
            }
        });

        Schema::table('articles', function (Blueprint $table) {
            foreach ([
                'featured_image_id',
                'image_text_block_id',
                'image_magazine_id',
            ] as $column) {
                $table->dropForeign([$column]);
                $table->foreign($column)
                    ->references('id')
                    ->on('media_items')
                    ->onDelete('CASCADE')
                    ->onUpdate('CASCADE');
            }
        });

        Schema::table('events', function (Blueprint $table) {
            $table->dropForeign(['featured_image_id']);
            $table->foreign('featured_image_id')
                ->references('id')
                ->on('media_items')
                ->onDelete('CASCADE')
                ->onUpdate('CASCADE');
        });
    }
};
