<?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
    {
        AgentDefaultRolePermissions::ensurePlatformAgentRole();

        AgentNode::query()->each(static function (AgentNode $node): void {
            $ownerRole = AdminRole::query()
                ->where('owner_agent_id', $node->id)
                ->where('slug', 'agent_owner_'.$node->id)
                ->first();

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

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

    public function down(): void
    {
        // 产品策略调整，回滚不恢复报表中心权限。
    }
};
