make(Kernel::class)->bootstrap(); if (! $app->environment('local', 'testing') || DB::connection()->getDriverName() !== 'pgsql') { throw new RuntimeException('Local/testing PostgreSQL required.'); } $db = DB::connection(); $report = $db->transaction(function () use ($db): array { $db->statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY'); $tables = $db->select("select tablename from pg_tables where schemaname='public' order by tablename"); $counts = []; foreach ($tables as $table) { $counts[$table->tablename] = $db->table($table->tablename)->count(); } return [ 'generated_at' => date(DATE_ATOM), 'php' => PHP_VERSION, 'postgres' => $db->selectOne('show server_version')->server_version, 'postgis' => $db->selectOne('select postgis_lib_version() as version')->version, 'table_counts' => $counts, 'checks' => [ 'invalid_geometries' => $db->table('geo_geometries')->whereRaw('NOT ST_IsValid(geom) OR ST_IsEmpty(geom) OR ST_SRID(geom) <> 4326')->count(), 'duplicate_opportunity_references' => count($db->select('select reference from opportunities group by reference having count(*) > 1')), 'opportunities_without_promoter' => $db->table('opportunities')->whereRaw("promoter_name IS NULL OR trim(promoter_name) = ''")->count(), 'opportunities_without_profile_source' => $db->table('opportunities as o')->leftJoin('opportunity_profiles as p', 'p.opportunity_id', '=', 'o.id')->whereRaw("p.source_reference IS NULL OR trim(p.source_reference) = ''")->count(), 'projects_without_description' => $db->table('projects')->whereNull('description')->count(), ], 'limits' => ['No semantic deduplication or legal verification performed.', 'Empty geometry inventory does not demonstrate official perimeter coverage.', 'Document orphan and project promoter checks require explicit ownership mapping in phase 1.'], ]; }); echo json_encode($report, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR).PHP_EOL;