-- 005: corriger les checkpoints + backfill enriched depuis les données de l'ancien scraper

-- 1. Renommer la clé checkpoint morte 'last_additions_check' → 'last_created_check'
--    (le code utilise last_{order_by}_check donc 'last_created_check', pas 'last_additions_check')
INSERT INTO crawl_checkpoint (key, value)
  VALUES ('last_created_check', NULL)
  ON CONFLICT (key) DO NOTHING;

DELETE FROM crawl_checkpoint WHERE key = 'last_additions_check';

-- 2. Backfill depuis les données de l'ancien scraper :
--    85 829 bands ont data->'band_page' rempli mais enriched=false et colonnes top-level vides.
--    On remplit les colonnes à partir du JSONB existant.
UPDATE bands SET
  enriched   = true,
  status     = COALESCE(
                 NULLIF(trim(status), ''),
                 data->'band_page'->>'status'
               ),
  genre      = COALESCE(
                 NULLIF(trim(genre), ''),
                 data->'band_page'->>'genre',
                 data->'band_page'->'info_raw'->>'genre'
               ),
  themes     = COALESCE(
                 NULLIF(trim(themes), ''),
                 NULLIF(data->'band_page'->>'lyrical_themes', 'N/A'),
                 NULLIF(data->'band_page'->'info_raw'->>'themes', 'N/A')
               ),
  formed_year = COALESCE(
                 formed_year,
                 CASE
                   WHEN (data->'band_page'->>'formed_in') ~ '^\d{4}$'
                     THEN (data->'band_page'->>'formed_in')::int
                   WHEN (data->'band_page'->>'formed_in') IS NOT NULL
                     THEN (regexp_match(data->'band_page'->>'formed_in', '\d{4}'))[1]::int
                   WHEN (data->'band_page'->'info_raw'->>'formed in') IS NOT NULL
                     THEN (regexp_match(data->'band_page'->'info_raw'->>'formed in', '\d{4}'))[1]::int
                   ELSE NULL
                 END
               ),
  location_text = COALESCE(
                    NULLIF(trim(location_text), ''),
                    data->'band_page'->>'location',
                    data->'band_page'->'info_raw'->>'location'
                  )
WHERE data->'band_page' IS NOT NULL;
