-- EN site_pages satırları + routes (TR sistem sayfalarının localized slug karşılığı)
USE faunamix;

SET @has_locale = (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'site_pages' AND COLUMN_NAME = 'locale'
);

-- EN sayfa satırları (TR kopyası — panelden metin güncellenir)
SET @sql = IF(
  @has_locale > 0,
  "INSERT INTO site_pages (
    parent_id, sort_order, path, locale, page_template, title, short_title, excerpt, image_url, cover_url, body_html,
    meta_title, meta_keywords, meta_description, is_active, legal_review_confirmed, legal_review_note
  )
  SELECT
    sp.parent_id,
    sp.sort_order,
    CASE TRIM(sp.path)
      WHEN '/iletisim' THEN '/contact'
      WHEN '/hakkimizda' THEN '/about-us'
      WHEN '/gizlilik' THEN '/privacy'
      WHEN '/kullanim' THEN '/terms'
      WHEN '/mesafeli-satis' THEN '/distance-selling'
      WHEN '/on-bilgilendirme' THEN '/pre-contract-info'
      WHEN '/iade-kosullari' THEN '/returns'
      WHEN '/rezervasyon' THEN '/reservation'
      ELSE TRIM(sp.path)
    END,
    'en',
    sp.page_template,
    sp.title,
    sp.short_title,
    sp.excerpt,
    sp.image_url,
    sp.cover_url,
    sp.body_html,
    sp.meta_title,
    sp.meta_keywords,
    sp.meta_description,
    sp.is_active,
    sp.legal_review_confirmed,
    sp.legal_review_note
  FROM site_pages sp
  WHERE sp.locale = ''
    AND TRIM(sp.path) IN (
      '/iletisim', '/hakkimizda', '/gizlilik', '/kullanim', '/mesafeli-satis',
      '/on-bilgilendirme', '/iade-kosullari', '/rezervasyon'
    )
    AND NOT EXISTS (
      SELECT 1 FROM site_pages en
      WHERE en.locale = 'en'
        AND en.path = CASE TRIM(sp.path)
          WHEN '/iletisim' THEN '/contact'
          WHEN '/hakkimizda' THEN '/about-us'
          WHEN '/gizlilik' THEN '/privacy'
          WHEN '/kullanim' THEN '/terms'
          WHEN '/mesafeli-satis' THEN '/distance-selling'
          WHEN '/on-bilgilendirme' THEN '/pre-contract-info'
          WHEN '/iade-kosullari' THEN '/returns'
          WHEN '/rezervasyon' THEN '/reservation'
          ELSE TRIM(sp.path)
        END
    )",
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- EN routes (sayfa entity)
INSERT INTO routes (locale, path, type, entity_id, redirect_to, is_active, priority, meta_title, meta_description)
SELECT 'en', TRIM(sp.path), 'page', sp.id, NULL, sp.is_active, 50, sp.meta_title, sp.meta_description
FROM site_pages sp
WHERE sp.locale = 'en'
  AND TRIM(sp.path) IN (
    '/contact', '/about-us', '/privacy', '/terms', '/distance-selling',
    '/pre-contract-info', '/returns', '/reservation'
  )
  AND NOT EXISTS (SELECT 1 FROM routes r WHERE r.locale = 'en' AND r.path = TRIM(sp.path));

-- Eski EN URL: TR slug → localized slug (301)
INSERT INTO routes (locale, path, type, entity_id, redirect_to, redirect_status, is_active, priority)
SELECT 'en', v.tr_path, 'page', NULL, v.en_path, 301, 1, 50
FROM (
  SELECT '/iletisim' AS tr_path, '/contact' AS en_path
  UNION ALL SELECT '/hakkimizda', '/about-us'
  UNION ALL SELECT '/gizlilik', '/privacy'
  UNION ALL SELECT '/kullanim', '/terms'
  UNION ALL SELECT '/mesafeli-satis', '/distance-selling'
  UNION ALL SELECT '/on-bilgilendirme', '/pre-contract-info'
  UNION ALL SELECT '/iade-kosullari', '/returns'
  UNION ALL SELECT '/rezervasyon', '/reservation'
) AS v
WHERE NOT EXISTS (SELECT 1 FROM routes r WHERE r.locale = 'en' AND r.path = v.tr_path);
