-- 007: champs verrouillés (éditions manuelles), conflits crawler, déclencheurs de jobs,
--      et nettoyage des 6852 bands non-européens entrés via le crawl incrémental global.

-- 1. Protection des éditions manuelles
ALTER TABLE bands
  ADD COLUMN IF NOT EXISTS locked_fields   JSONB NOT NULL DEFAULT '{}',
  ADD COLUMN IF NOT EXISTS crawler_pending JSONB;

-- 2. Table de déclencheurs de jobs (depuis le dashboard admin sans socket Docker)
CREATE TABLE IF NOT EXISTS job_triggers (
  id           BIGSERIAL PRIMARY KEY,
  job_type     TEXT NOT NULL,  -- 'enrich'|'incremental'|'full_crawl'|'geocode'
  status       TEXT NOT NULL DEFAULT 'pending',  -- pending|running|done|error
  requested_by TEXT,
  created_at   TIMESTAMPTZ NOT NULL DEFAULT now(),
  started_at   TIMESTAMPTZ,
  finished_at  TIMESTAMPTZ,
  error        TEXT
);
CREATE INDEX IF NOT EXISTS idx_job_triggers_pending ON job_triggers (status, created_at)
  WHERE status = 'pending';

-- 3. Suppression des bands hors Europe (entrés via crawl incrémental global)
--    Liste officielle des codes pays européens utilisés par Metal Archives.
DELETE FROM bands
WHERE country IS NOT NULL
  AND country NOT IN (
    'AD','AL','AT','BA','BE','BG','BY','CH','CY','CZ',
    'DE','DK','EE','ES','FI','FR','GB','GE','GR','HR',
    'HU','IE','IS','IT','LI','LT','LU','LV','MC','MD',
    'ME','MK','MT','NL','NO','PL','PT','RO','RS','RU',
    'SE','SI','SK','SM','TR','UA','VA','XK'
  );
