<?php

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

return new class extends Migration
{
    /**
     * Backfill Manager workspace_user rows that have null or empty permissions
     * with the defaults from config('permissions.defaults.manager').
     */
    public function up(): void
    {
        $defaults = json_encode(config('permissions.defaults.manager'));

        DB::table('workspace_user')
            ->where('role', 'manager')
            ->where(function ($query) {
                $query->whereNull('permissions')
                    ->orWhere('permissions', '[]')
                    ->orWhere('permissions', 'null')
                    ->orWhere('permissions', '');
            })
            ->update(['permissions' => $defaults]);
    }

    /**
     * Reverse the migration (no-op — we cannot know original values).
     */
    public function down(): void
    {
        // Cannot reverse: original null/empty values are indistinguishable
    }
};
