-- 062_clean_avito_addresses.sql
-- Strip CSS noise from listings.address for Avito source (one-shot backfill).
-- Idempotent: re-run is safe (regexp_replace on already-clean string is a no-op).
BEGIN;

UPDATE listings
   SET address = trim(both ' ,.' FROM
                       regexp_replace(
                         regexp_replace(address, '\.?css-[a-z0-9_-]+\s*\{[^}]*\}', '', 'gi'),
                         '\s*(Площадь \d+(\.\d+)?\s?(м²|м2|кв\.?\s?м)|от \d+\s?мин\.|css-[a-z0-9_-]+).*$', '', 'i'
                       ))
 WHERE source = 'avito'
   AND (address ~* 'css-[a-z0-9_-]+' OR address ~* 'Площадь \d+(\.\d+)?\s?(м²|м2|кв\.?\s?м)|от \d+\s?мин\.');

COMMIT;
