from pathlib import Path import json out=Path(__file__).resolve().parent;qa=Path('C:/Users/macbook/.config/investmoz/parques-L1-QA-20261016/source');root=out.parents[1] def change(n,old,new): p=qa/n;s=p.read_text('utf-8') if new in s and old not in s:return assert s.count(old)==1,(n,old[:80]);p.write_text(s.replace(old,new),'utf-8') change('app/Models/InvestmentZone.php',"protected $hidden = ['editorial_draft'];","protected $hidden = ['editorial_draft', 'operational_details'];") change('app/Models/InvestmentZone.php',"protected $casts = [", "protected $casts = ['operational_details' => 'array', ") change('app/Services/InvestmentZoneOperationalDetails.php','array $before, array $input): array','array $before, array $input, ?string $operatorId): array') change('app/Services/InvestmentZoneOperationalDetails.php',"$draft=app(InvestmentZoneOperationalProfile::class)->draft($zone);$operator=$draft['fields']['operator_id'] ?? $zone->operator_id;","$operator=$operatorId;") change('app/Services/InvestmentZoneOperationalProfile.php',"'has_private_draft'=>$draft !== null, 'profile_available'=>$this->installed()];", "'has_private_draft'=>$draft !== null, 'profile_available'=>$this->installed(),\n 'operational_details'=>($draft['fields']['operational_details'] ?? app(InvestmentZoneOperationalDetails::class)->recover($zone)),\n 'details_available'=>app(InvestmentZoneOperationalDetails::class)->installed()];") change('app/Services/InvestmentZoneOperationalProfile.php',"unset($record['editorial_draft']);","unset($record['editorial_draft'], $record['operational_details']);") change('app/Services/InvestmentZoneOperationalProfile.php',"$fields=[...$base,...array_intersect_key($data,array_flip(self::FIELDS))];", "$fields=[...$base,...array_intersect_key($data,array_flip(self::FIELDS))];\n $details=app(InvestmentZoneOperationalDetails::class);\n if (array_key_exists('operational_details',$data)) {\n $fields['operational_details']=$details->merge($zone,$base['operational_details'] ?? $details->recover($zone),$data['operational_details'],$fields['operator_id'] ?? null);\n } elseif ($details->stored($zone) && !array_key_exists('operational_details',$fields)) {\n $fields['operational_details']=$details->recover($zone);\n }") change('app/Services/InvestmentZoneOperationalProfile.php',"'profile_prepared',['public'=>$zone->toArray(),'draft'=>$draft]", "'profile_prepared',['public'=>[...$zone->toArray(),'operational_details'=>$details->stored($zone)],'draft'=>$draft]") change('app/Services/InvestmentZoneOperationalProfile.php',"// Support identifiers and private notes stay in the encrypted immutable preparation snapshot.", "if (array_key_exists('operational_details',$draft['fields'])) {\n $zone->setAttribute('operational_details',app(InvestmentZoneOperationalDetails::class)->pack($draft['fields']['operational_details']));\n }\n // Existing resource support remains in the encrypted snapshot; L1 private fields are durable.") # Reconcile snapshots must include hidden durable publication too. change('app/Services/InvestmentZoneOperationalProfile.php',"['public'=>$zone->toArray(),'draft'=>$draft]", "['public'=>[...$zone->toArray(),'operational_details'=>app(InvestmentZoneOperationalDetails::class)->stored($zone)],'draft'=>$draft]") change('app/Services/InvestmentZones.php',"$json = $zone->toJson(JSON_THROW_ON_ERROR);", "$json = json_encode([...$zone->toArray(),'operational_details'=>app(InvestmentZoneOperationalDetails::class)->stored($zone)],JSON_THROW_ON_ERROR);") change('app/Services/InvestmentZones.php',"$zone->fill([...$data, 'editor_id'", "abort_if(array_key_exists('operational_details',$data),422,'Prepare L1 numa ficha de parque existente; não cria outro cadastro.');\n $zone->fill([...$data, 'editor_id'") # Safely attach only a materialized publication; raw hidden JSON never enters a response. change('app/Http/Controllers/InvestmentZoneController.php',"$publicFields = [", "$zone->setAttribute('operational_details_public',app(\\App\\Services\\InvestmentZoneOperationalDetails::class)->publicData($zone));\n $publicFields = ['operational_details_public', ") change('app/Http/Controllers/InvestmentZoneController.php',"$data = $request->validate([", "$data = $request->validate([",) if False else None # validation anchor unique in save change('app/Http/Controllers/InvestmentZoneController.php',"'activities' => ['sometimes', 'array', 'max:50']", "'operational_details' => ['sometimes','array'],\n 'activities' => ['sometimes', 'array', 'max:50']") change('app/Http/Controllers/InvestmentZoneController.php',"$publicColumns = array_values(array_filter", "if(app(\\App\\Services\\InvestmentZoneOperationalDetails::class)->installed()){$publicColumns[]='operational_details';}\n $publicColumns = array_values(array_filter") change('app/Http/Controllers/InvestmentZoneController.php',"return $management ? $zone : app(InvestmentZoneOperationalProfile::class)->publicDetails($zone->toArray());", "return $management ? $zone : [...app(InvestmentZoneOperationalProfile::class)->publicDetails($zone->toArray()),'operational_details_public'=>app(\\App\\Services\\InvestmentZoneOperationalDetails::class)->publicData($zone)];") change('app/Http/Controllers/InvestmentZoneController.php',"$props = ['installation' =>", "$props = ['detailsInstallation'=>$management ? app(\\App\\Services\\InvestmentZoneOperationalDetailsInstallation::class)->preview($request->user()) : null, 'installation' =>") # Install operation keeps existing authority/MFA/readiness, but has its own nominal path and binding. s=(qa/'app/Services/InvestmentZoneProfileInstallation.php').read_text('utf-8') s=s.replace('InvestmentZoneProfileInstallation','InvestmentZoneOperationalDetailsInstallation').replace('InvestmentZoneOperationalProfile::MIGRATION','InvestmentZoneOperationalDetails::MIGRATION') s=s.replace('investment-zone-profile-installation','investment-zone-details-installation').replace('investment-zone-profile.authenticated_installation','investment-zone-details.authenticated_installation').replace('park-profile/readiness.json','park-details/readiness.json').replace('editorial_draft','operational_details').replace("$shape->data_type==='text'","$shape->data_type==='jsonb'").replace('161026,130000','161026,140000') start=s.index(" return ['app/Models/");end=s.index('\n }',start) paths=[x['path']for x in json.loads((root/'outputs/investmoz-parques-integral-delimitacao-20261016/DOC01/FICHEIROS-PREVISTOS.json').read_text('utf-8'))['sources']] s=s[:start]+' return '+str(paths).replace('[','[',1)+';'+s[end:] s=s.replace("$state=['target'", "if(!app(InvestmentZoneOperationalProfile::class)->installed() || DB::table('migrations')->where('migration',InvestmentZoneOperationalProfile::MIGRATION)->count()!==1){$reasons[]='Base 130000 ausente ou divergente; instalar/verificar antes de L1.';}\n $state=['target'") (qa/'app/Services/InvestmentZoneOperationalDetailsInstallation.php').write_text(s,'utf-8') change('routes/investment-zones.php'," Route::get('/management/investment/zones/profile-install'", " Route::get('/management/investment/zones/operational-install',function(\\Illuminate\\Http\\Request $r,\\App\\Services\\InvestmentZoneOperationalDetailsInstallation $s){return response()->json(['plan'=>$s->preview($r->user())]);});\n Route::post('/management/investment/zones/operational-install',function(\\Illuminate\\Http\\Request $r,\\App\\Services\\InvestmentZoneOperationalDetailsInstallation $s){return response()->json(['receipt'=>$s->apply($r->user(),$r->all())]);});\n Route::get('/management/investment/zones/profile-install'") print('L1 core adapted in private QA only')