UPDATE app_settings
SET value_json = to_jsonb(
  trim(
    regexp_replace(
      value_json #>> '{}',
      E'\\n?OUTPUT:[[:space:]]*JSON only\\.[[:space:]]*',
      E'\\n',
      'gi'
    )
  )
),
updated_at = NOW()
WHERE key = 'ai_writer_prompt'
  AND value_json #>> '{}' ~* 'OUTPUT:[[:space:]]*JSON only\.';

UPDATE app_settings
SET value_json = to_jsonb(
  CASE
    WHEN value_json #>> '{}' ~* 'Return JSON only\\.'
      THEN value_json #>> '{}'
    ELSE trim(value_json #>> '{}') || E'\n- Return JSON only. No markdown. No text outside JSON.'
  END
),
updated_at = NOW()
WHERE key = 'ai_writer_contract';
