reference($choice['source']); abort_unless($ref['kind'] === 'opportunity', 422, 'Seleccione uma ficha de oportunidade.'); $record = Opportunity::findOrFail($ref['id']); $old = collect($previous)->first(fn ($row) => $row['opportunity']['key'] === $ref['key']); $corridors = app(InvestmentCorridorContext::class); $corridor = $choice['corridor_key'] ?? $old['corridor_key'] ?? $corridors->key($record); $provinces = $corridor ? $corridors->provinces($corridor) : []; $documented = $record->primary_territory_id; $territory = $choice['territory'] ?? ($documented ? 'territory:'.$documented : null); if (!$territory && count($provinces) === 1) $territory = 'territory:'.$provinces[0]->id; if ($corridor && $territory) abort_unless($corridors->compatible($corridor, substr($territory, 10)), 422, 'A localização não pertence ao corredor escolhido.'); $previousTerritory = $old['territory']['key'] ?? null; $newAlternative = $documented && $territory !== 'territory:'.$documented && (! $old || ($old['documented_territory_id'] ?? null) !== $documented || $territory !== $previousTerritory); $corridorChanged = $old && ($old['corridor_key'] ?? $corridors->key($record)) !== $corridor; $incompatibleOld = $corridorChanged && $previousTerritory && !$corridors->compatible($corridor, substr($previousTerritory, 10)); if (!$pending && ($newAlternative || ($previousTerritory && $territory !== $previousTerritory && !$incompatibleOld))) { abort_unless(! empty($choice['confirm_location_change']) && mb_strlen(trim($choice['location_reason'] ?? '')) >= 20, 422, 'Uma localização documentada ou já confirmada só muda por escolha explícita, com confirmação e motivo.'); } $location = $territory ? app(InvestorContext::class)->reference($territory) : null; abort_unless($location === null || $location['kind'] === 'territory', 422); $result[] = ['opportunity' => $ref, 'territory' => $location, 'corridor_key' => $corridor, 'corridor_name' => $corridor ? config('investment_corridors.'.$corridor.'.name') : null, 'allowed_provinces' => array_map(fn($p)=>(array)$p, $provinces), 'territory_path' => $location ? array_map(fn ($node) => \Illuminate\Support\Arr::only((array) $node, ['id', 'name', 'parent_id', 'location_type']), app(InvestmentGeography::class)->path($location['id'], true)) : [], 'activities' => $choice['activities'] ?? ($old['activities'] ?? []), 'documented_territory_id' => $documented, 'location_reason' => $choice['location_reason'] ?? '', 'location_state' => $location ? ($documented && $territory === 'territory:'.$documented ? 'documented' : 'investor_selected') : 'undetermined', 'location_changed_explicitly' => $old && $territory === $previousTerritory ? (bool) ($old['location_changed_explicitly'] ?? false) : (bool) ($choice['confirm_location_change'] ?? false), 'coverage' => data_get($record->workflow_context, 'corridor_agribusiness.corridor'), 'source' => data_get($record->workflow_context, 'corridor_agribusiness.source'), 'source_sha256' => data_get($record->workflow_context, 'corridor_agribusiness.source_sha256')]; } return $result; } }