<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    public function up(): void
    {
        DB::table('website_type_extras')
            ->select(['id', 'config_schema'])
            ->orderBy('id')
            ->each(function (object $definition): void {
                $schema = json_decode($definition->config_schema, true);

                if (! is_array($schema) || ! array_key_exists('database_rules', $schema)) {
                    return;
                }

                unset($schema['database_rules']);

                DB::table('website_type_extras')
                    ->where('id', $definition->id)
                    ->update(['config_schema' => json_encode($schema)]);
            });
    }

    public function down(): void
    {
        // Removed rules cannot be reconstructed generically.
    }
};
