<?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('popup_announcements', function (Blueprint $table): void {
            if (! Schema::hasColumn('popup_announcements', 'target_audience')) {
                $table->string('target_audience', 30)->default('all')->after('message')->index();
            }
            if (! Schema::hasColumn('popup_announcements', 'display_surface')) {
                $table->string('display_surface', 30)->default('both')->after('target_audience')->index();
            }
            if (! Schema::hasColumn('popup_announcements', 'media_type')) {
                $table->string('media_type', 20)->default('none')->after('display_surface');
            }
            if (! Schema::hasColumn('popup_announcements', 'media_path')) {
                $table->string('media_path')->nullable()->after('media_type');
            }
            if (! Schema::hasColumn('popup_announcements', 'video_url')) {
                $table->string('video_url')->nullable()->after('media_path');
            }
            if (! Schema::hasColumn('popup_announcements', 'display_order')) {
                $table->unsignedInteger('display_order')->default(0)->after('video_url')->index();
            }
        });
    }

    public function down(): void
    {
        Schema::table('popup_announcements', function (Blueprint $table): void {
            if (Schema::hasColumn('popup_announcements', 'display_order')) {
                $table->dropColumn('display_order');
            }
            if (Schema::hasColumn('popup_announcements', 'video_url')) {
                $table->dropColumn('video_url');
            }
            if (Schema::hasColumn('popup_announcements', 'media_path')) {
                $table->dropColumn('media_path');
            }
            if (Schema::hasColumn('popup_announcements', 'media_type')) {
                $table->dropColumn('media_type');
            }
            if (Schema::hasColumn('popup_announcements', 'display_surface')) {
                $table->dropColumn('display_surface');
            }
            if (Schema::hasColumn('popup_announcements', 'target_audience')) {
                $table->dropColumn('target_audience');
            }
        });
    }
};
