where('slug', $planSlug) ->first(); if (! $plan) { throw new \InvalidArgumentException("Plan not found: {$planSlug}"); } $resolved = $this->resolvePhase($plan, $phase); if (! $resolved) { throw new \InvalidArgumentException("Phase not found: {$phase}"); } return $resolved; } private function resolvePhase(AgentPlan $plan, string|int $identifier): ?AgentPhase { if (is_numeric($identifier)) { return $plan->agentPhases()->where('order', (int) $identifier)->first(); } return $plan->agentPhases() ->where(function ($query) use ($identifier) { $query->where('name', $identifier) ->orWhere('order', $identifier); }) ->first(); } }