ALTER TABLE qc_template_fields
ADD COLUMN website_section TEXT NOT NULL DEFAULT 'other';

UPDATE qc_template_fields
SET website_section = CASE
  WHEN lower(field_kind) = 'image'
    OR lower(section) LIKE '%gallery%'
    OR lower(section) LIKE '%media%'
    OR lower(key_name) LIKE '%gallery%'
    OR lower(key_name) LIKE '%image%'
    OR lower(key_name) LIKE '%photo%'
    OR lower(path) LIKE '%gallery%'
    OR lower(path) LIKE '%image%'
    OR lower(path) LIKE '%photo%'
    THEN 'gallery'
  WHEN lower(section) LIKE '%testimonial%'
    OR lower(key_name) LIKE '%testimonial%'
    OR lower(path) LIKE '%testimonial%'
    THEN 'testimonials'
  WHEN lower(section) = 'services'
    AND (lower(key_name) LIKE '%item%' OR lower(key_name) GLOB '*_[0-9]*')
    THEN 'service_item'
  WHEN lower(section) = 'services'
    OR lower(key_name) LIKE '%service%'
    OR lower(path) LIKE '%service%'
    THEN 'services'
  WHEN lower(section) LIKE '%team%'
    OR lower(key_name) LIKE '%team%'
    OR lower(key_name) LIKE '%member%'
    OR lower(path) LIKE '%team%'
    OR lower(path) LIKE '%member%'
    THEN 'team'
  WHEN lower(section) LIKE '%hero%'
    OR lower(key_name) LIKE '%hero%'
    OR lower(path) LIKE '%hero%'
    OR lower(key_name) LIKE '%welcome%'
    OR lower(path) LIKE '%welcome%'
    OR lower(key_name) LIKE '%banner%'
    OR lower(path) LIKE '%banner%'
    THEN 'hero'
  WHEN lower(section) LIKE '%intro%'
    OR lower(key_name) LIKE '%intro%'
    OR lower(path) LIKE '%intro%'
    THEN 'intro'
  WHEN lower(section) LIKE '%about%'
    OR lower(key_name) LIKE '%about%'
    OR lower(path) LIKE '%about%'
    OR length(sample_value) > 180
    THEN 'about'
  WHEN lower(section) LIKE '%cta%'
    OR lower(key_name) LIKE '%cta%'
    OR lower(path) LIKE '%cta%'
    OR lower(key_name) LIKE '%button%'
    OR lower(path) LIKE '%button%'
    OR lower(key_name) LIKE '%highlight%'
    OR lower(path) LIKE '%highlight%'
    THEN 'cta'
  WHEN lower(section) LIKE '%contact%'
    OR lower(key_name) LIKE '%contact%'
    OR lower(path) LIKE '%contact%'
    OR lower(key_name) LIKE '%email%'
    OR lower(path) LIKE '%email%'
    OR lower(key_name) LIKE '%phone%'
    OR lower(path) LIKE '%phone%'
    THEN 'contact'
  WHEN lower(section) LIKE '%footer%'
    OR lower(key_name) LIKE '%footer%'
    OR lower(path) LIKE '%footer%'
    THEN 'footer'
  ELSE 'other'
END;
