join('public.sectors as s', 's.id', '=', 'l.sector_id') ->where('l.sector_id', $legacyId)->where('l.resolver_version', self::VERSION) ->where('l.status', 'active')->where('s.is_active', true)->whereNull('s.deleted_at') ->whereColumn('l.source_code', 's.slug')->where('l.valid_from', '<=', now()) ->where(fn ($q) => $q->whereNull('l.valid_until')->orWhere('l.valid_until', '>', now())) ->value('l.canonical_sector_id'); return is_string($id) ? $id : null; } }