<?php

use App\Models\Room;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        Room::withTrashed()
            ->whereNotNull('settings')
            ->get()
            ->each(function (Room $room): void {
                $settings = $room->settings;
                if (! is_array($settings) || empty($settings['invite_only'])) {
                    return;
                }

                $settings['invite_only'] = false;
                $room->forceFill(['settings' => $settings])->saveQuietly();
            });
    }

    public function down(): void
    {
        // Prior invite-only state is not restored.
    }
};
