<?php

use App\Models\AdminRole;
use App\Models\AgentNode;
use App\Support\AgentDefaultRolePermissions;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        AgentNode::query()->each(static function (AgentNode $node): void {
            $role = AdminRole::query()
                ->where('owner_agent_id', $node->id)
                ->where('slug', 'agent_owner_'.$node->id)
                ->first();

            if ($role === null) {
                return;
            }

            $role->syncLegacyPermissionSlugs(AgentDefaultRolePermissions::ownerSlugsForNode($node));
        });
    }

    public function down(): void
    {
        // 权限包为产品策略，回滚不恢复旧 slug 集合。
    }
};
