INSERT INTO pages (
    internal_code, title, slug, page_type, excerpt, body, status, canonical_url, created_at, updated_at, published_at
)
SELECT
    'PAGE-CUSTOM-DEV',
    'Developpement sur mesure',
    'developpement-sur-mesure',
    'institutional',
    'Rubrique institutionnelle Developpement sur mesure.',
    'Page Developpement sur mesure a completer depuis le back-office.',
    'draft',
    '/pages/developpement-sur-mesure/',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP,
    NULL
WHERE NOT EXISTS (
    SELECT 1
    FROM pages
    WHERE slug = 'developpement-sur-mesure'
);

INSERT INTO redirects (source_path, target_url, status_code, is_active, note, created_at, updated_at)
SELECT
    '/cfi',
    '/pages/cfi/',
    301,
    1,
    'Alias public CFI',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
WHERE NOT EXISTS (
    SELECT 1
    FROM redirects
    WHERE source_path = '/cfi'
);

INSERT INTO redirects (source_path, target_url, status_code, is_active, note, created_at, updated_at)
SELECT
    '/mobilityscan',
    '/pages/mobilityscan/',
    301,
    1,
    'Alias public MobilityScan',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
WHERE NOT EXISTS (
    SELECT 1
    FROM redirects
    WHERE source_path = '/mobilityscan'
);

UPDATE page_sections ps
JOIN pages p ON p.id = ps.page_id
SET ps.settings_json = REPLACE(
    ps.settings_json,
    '"url":"","link_label":"Decouvrir MobilityScan"',
    '"url":"/mobilityscan","link_label":"Decouvrir MobilityScan"'
)
WHERE p.slug = 'home'
  AND ps.section_type = 'home-ecosystem'
  AND ps.settings_json LIKE '%"title":"MobilityScan"%'
  AND ps.settings_json LIKE '%"url":"","link_label":"Decouvrir MobilityScan"%';
