-- Keep the AI-writer divider deterministic.
-- Target separator length: 17 characters.

UPDATE app_settings
SET value_json = to_jsonb(
    regexp_replace(
        value_json #>> '{}',
        'Черта-разделитель:[^\n]*',
        'Черта-разделитель: отдельная строка ровно из 17 символов. Используй строго эту строку без изменений: ━━━━━━━━━━━━━━━━━',
        'g'
    )
),
updated_at = NOW()
WHERE key = 'ai_writer_prompt'
  AND (value_json #>> '{}') LIKE '%Черта-разделитель%';

UPDATE app_settings
SET value_json = to_jsonb(
    CASE
      WHEN (value_json #>> '{}') LIKE '%Separator line must be exactly%'
        THEN regexp_replace(
          value_json #>> '{}',
          'Separator line must be exactly[^\n]*',
          'Separator line must be exactly this 17-character line when used: ━━━━━━━━━━━━━━━━━',
          'g'
        )
      ELSE (value_json #>> '{}') || E'\n- Separator line must be exactly this 17-character line when used: ━━━━━━━━━━━━━━━━━'
    END
),
updated_at = NOW()
WHERE key = 'ai_writer_contract';

UPDATE raw_posts
SET rewritten_text = CASE
        WHEN rewritten_text IS NULL THEN NULL
        ELSE regexp_replace(rewritten_text, E'^[━─—-]{3,}\\s*$', '━━━━━━━━━━━━━━━━━', 'gm')
    END,
    final_text = CASE
        WHEN final_text IS NULL THEN NULL
        ELSE regexp_replace(final_text, E'^[━─—-]{3,}\\s*$', '━━━━━━━━━━━━━━━━━', 'gm')
    END,
    updated_at = NOW()
WHERE COALESCE(rewritten_text, '') ~ E'(?m)^[━─—-]{3,}\\s*$'
   OR COALESCE(final_text, '') ~ E'(?m)^[━─—-]{3,}\\s*$';
