<?php

declare(strict_types=1);

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('user_profiles')) {
            return;
        }

        Schema::table('user_profiles', function (Blueprint $table) {
            if (!Schema::hasColumn('user_profiles', 'email_notifications')) {
                $table->boolean('email_notifications')->default(true)->after('auto_post_upload');
            }

            if (!Schema::hasColumn('user_profiles', 'upload_notifications')) {
                $table->boolean('upload_notifications')->default(true)->after('email_notifications');
            }

            if (!Schema::hasColumn('user_profiles', 'follower_notifications')) {
                $table->boolean('follower_notifications')->default(true)->after('upload_notifications');
            }

            if (!Schema::hasColumn('user_profiles', 'comment_notifications')) {
                $table->boolean('comment_notifications')->default(true)->after('follower_notifications');
            }

            if (!Schema::hasColumn('user_profiles', 'newsletter')) {
                $table->boolean('newsletter')->default(false)->after('comment_notifications');
            }
        });
    }

    public function down(): void
    {
        if (!Schema::hasTable('user_profiles')) {
            return;
        }

        Schema::table('user_profiles', function (Blueprint $table) {
            foreach (['newsletter', 'comment_notifications', 'follower_notifications', 'upload_notifications', 'email_notifications'] as $column) {
                if (Schema::hasColumn('user_profiles', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
