

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'company', 'name', 'WININFO', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'company' AND key_name = 'name');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'company', 'phone', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'company' AND key_name = 'phone');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'company', 'email', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'company' AND key_name = 'email');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'links', 'hotline', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'links' AND key_name = 'hotline');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'links', 'client_space', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'links' AND key_name = 'client_space');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'social', 'linkedin', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'social' AND key_name = 'linkedin');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'primary', '#1657c0', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'primary');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'primary_dark', '#12367d', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'primary_dark');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'primary_soft', '#dbe7ff', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'primary_soft');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'accent', '#1f8f73', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'accent');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'text', '#162033', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'text');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'text_soft', '#5f6d84', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'text_soft');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'background', '#f5f7fb', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'background');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'background_strong', '#ecf1f8', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'background_strong');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'surface', '#ffffff', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'surface');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'border', '#d6dfec', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'border');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'theme', 'radius', '22px', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'theme' AND key_name = 'radius');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'legal', 'mentions', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'legal' AND key_name = 'mentions');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'legal', 'privacy', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'legal' AND key_name = 'privacy');

INSERT INTO settings (group_name, key_name, value, created_at, updated_at)
SELECT 'legal', 'cookies', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE group_name = 'legal' AND key_name = 'cookies');

INSERT INTO module_types (code, name, slug, description, sort_order, created_at, updated_at)
SELECT 'metier', 'Métier', 'metier', 'Module destiné à une activité spécifique.', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM module_types WHERE code = 'metier');

INSERT INTO module_types (code, name, slug, description, sort_order, created_at, updated_at)
SELECT 'fonctionnel', 'Fonctionnel', 'fonctionnel', 'Fonction transverse ou complémentaire.', 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM module_types WHERE code = 'fonctionnel');

INSERT INTO module_types (code, name, slug, description, sort_order, created_at, updated_at)
SELECT 'transversal', 'Transversal', 'transversal', 'Capacité commune à plusieurs métiers.', 3, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM module_types WHERE code = 'transversal');

INSERT INTO categories (code, name, slug, sort_order, created_at, updated_at)
SELECT 'transport', 'Transport', 'transport', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'transport');

INSERT INTO categories (code, name, slug, sort_order, created_at, updated_at)
SELECT 'logistique', 'Logistique', 'logistique', 2, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'logistique');

INSERT INTO categories (code, name, slug, sort_order, created_at, updated_at)
SELECT 'travaux-publics', 'Travaux Publics', 'travaux-publics', 3, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'travaux-publics');

INSERT INTO categories (code, name, slug, sort_order, created_at, updated_at)
SELECT 'finance-pilotage', 'Finance & Pilotage', 'finance-pilotage', 4, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'finance-pilotage');

INSERT INTO categories (code, name, slug, sort_order, created_at, updated_at)
SELECT 'mobilite', 'Mobilité', 'mobilite', 5, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM categories WHERE slug = 'mobilite');

INSERT INTO products (
    internal_code, name, slug, tagline, short_description, presentation,
    status, featured, canonical_url, created_at, updated_at, published_at
)
SELECT 'PROD-001', 'Win@ERP', 'win-erp', 'Plateforme de gestion modulaire.', 'Produit principal WININFO.', 'Contenu initial de cadrage.', 'published', 1, '/solutions/win-erp/', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM products WHERE slug = 'win-erp');

INSERT INTO products (
    internal_code, name, slug, tagline, short_description, presentation,
    status, featured, canonical_url, created_at, updated_at, published_at
)
SELECT 'PROD-002', 'Win@WMS', 'win-wms', 'Gestion spécialisée d''entrepôt.', 'Produit principal WININFO.', 'Contenu initial de cadrage.', 'published', 1, '/solutions/win-wms/', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM products WHERE slug = 'win-wms');

INSERT INTO pages (
    internal_code, title, slug, page_type, excerpt, body, status, featured, canonical_url, created_at, updated_at, published_at
)
SELECT 'PAGE-001', 'Accueil', 'home', 'home', 'Page d''accueil provisoire pilotée par le back-office.', 'Le design final n''est pas livré dans cette phase.', 'published', 1, '/', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM pages WHERE slug = 'home');

INSERT INTO pages (
    internal_code, title, slug, page_type, excerpt, body, status, canonical_url, created_at, updated_at, published_at
)
SELECT 'PAGE-002', 'CFI', 'cfi', 'institutional', 'Rubrique institutionnelle CFI.', 'Page CFI à compléter depuis le back-office.', 'draft', '/pages/cfi/', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, NULL
WHERE NOT EXISTS (SELECT 1 FROM pages WHERE slug = 'cfi');

INSERT INTO pages (
    internal_code, title, slug, page_type, excerpt, body, status, canonical_url, created_at, updated_at, published_at
)
SELECT 'PAGE-003', 'MobilityScan', 'mobilityscan', 'institutional', 'Rubrique institutionnelle MobilityScan.', 'Page MobilityScan à compléter depuis le back-office.', 'draft', '/pages/mobilityscan/', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, NULL
WHERE NOT EXISTS (SELECT 1 FROM pages WHERE slug = 'mobilityscan');

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 menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'main', 'Accueil', 'custom', '/', 1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'main' AND label = 'Accueil');

INSERT INTO menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'main', 'Solutions', 'custom', '/catalogue/modules/', 2, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'main' AND label = 'Solutions');

INSERT INTO menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'main', 'CFI', 'custom', '/pages/cfi/', 3, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'main' AND label = 'CFI');

INSERT INTO menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'main', 'MobilityScan', 'custom', '/pages/mobilityscan/', 4, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'main' AND label = 'MobilityScan');

INSERT INTO menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'footer', 'Actualités', 'custom', '/blog/news/', 1, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'footer' AND label = 'Actualités');

INSERT INTO menu_items (location, label, link_type, url, sort_order, is_active, created_at, updated_at)
SELECT 'footer', 'Ressources', 'custom', '/blog/resource/', 2, 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
WHERE NOT EXISTS (SELECT 1 FROM menu_items WHERE location = 'footer' AND label = 'Ressources');

INSERT INTO page_sections (
    page_id, section_type, title, eyebrow, body, settings_json, sort_order, is_active, created_at, updated_at
)
SELECT
    home.id,
    defaults.section_type,
    defaults.title,
    defaults.eyebrow,
    defaults.body,
    defaults.settings_json,
    defaults.sort_order,
    1,
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
FROM (
    SELECT 'home-hero' AS section_type, 'Accueil' AS title, 'Solutions logicielles WININFO' AS eyebrow, 'Un front-office dynamique, alimente par le back-office et la base de donnees.' AS body, '{"cta_label":"Decouvrir les solutions","cta_url":"/catalogue/modules/","secondary_cta_label":"Voir les actualites","secondary_cta_url":"/blog/news/"}' AS settings_json, 1 AS sort_order
    UNION ALL
    SELECT 'home-intro', 'Une base editoriale structuree pour le front-office', 'Presentation WININFO', 'Les contenus institutionnels, produits, modules et ressources restent pilotes par le back-office existant.', '{}', 2
    UNION ALL
    SELECT 'home-solutions', 'Win@ERP et Win@WMS', 'Nos solutions', 'Les fiches produits publiees alimentent automatiquement cette section.', '{"cta_label":"Voir le catalogue","cta_url":"/catalogue/modules/","limit":"2"}', 3
    UNION ALL
    SELECT 'home-business-modules', 'Modules metier a la une', 'Vos metiers', 'Les modules metier mis en avant dans le back-office sont repris ici par ordre de publication.', '{"cta_label":"Explorer les modules","cta_url":"/catalogue/modules/?type=metier","limit":"6"}', 4
    UNION ALL
    SELECT 'home-functional-modules', 'Modules complementaires et transversaux', 'Modules fonctionnels', 'Les modules fonctionnels et transversaux restent relies a leur typologie en base.', '{"cta_label":"Voir les modules","cta_url":"/catalogue/modules/","limit":"6"}', 5
    UNION ALL
    SELECT 'home-custom-development', 'Une architecture prete pour les besoins specifiques', 'Developpement sur mesure', 'Le socle conserve une publication centralisee et peut accueillir les contenus dedies aux projets sur mesure dans le cadre defini.', '{"cta_label":"Decouvrir cette approche","cta_url":"/pages/developpement-sur-mesure/"}', 6
    UNION ALL
    SELECT 'home-ecosystem', 'Solutions, CFI et MobilityScan', 'Ecosysteme WININFO', 'Les activites complementaires de WININFO sont presentees via des cartes administrables.', '{"cards":[{"title":"CFI - Centre de Formation Informatique","description":"Formations professionnelles et accompagnement pour developper les competences de vos equipes.","url":"/cfi","link_label":"Decouvrir CFI","sort_order":1,"icon":"","media_id":"","is_active":1},{"title":"MobilityScan","description":"Equipements professionnels pour la mobilite, l identification, la tracabilite et la collecte de donnees.","url":"/mobilityscan","link_label":"Decouvrir MobilityScan","sort_order":2,"icon":"","media_id":"","is_active":1}]}', 7
    UNION ALL
    SELECT 'home-why-wininfo', 'Des contenus relies aux donnees publiees', 'Pourquoi WININFO ?', 'Les indicateurs, mises en avant et listes publiques sont alimentes par les donnees effectivement publiees.', '{}', 8
    UNION ALL
    SELECT 'home-approach', 'Un pilotage simple, sans page builder libre', 'Notre approche', 'La page d accueil repose sur des blocs types.
Les contenus restent parametres dans le back-office.
Les fiches produits et modules ne sont jamais recodees dans les templates.', '{}', 9
    UNION ALL
    SELECT 'home-latest-content', 'Les derniers contenus publies', 'Actualites et ressources', 'Les actualites et ressources sont reprises automatiquement selon leur date de publication.', '{"cta_label":"Voir tous les contenus","cta_url":"/blog/news/","limit":"3","source":"mixed"}', 10
    UNION ALL
    SELECT 'home-final-cta', 'Pilotez votre vitrine depuis le back-office', 'Passer a l action', 'Le design system, la navigation et la page d accueil sont maintenant relies au socle dynamique deja valide.', '{"cta_label":"Voir le catalogue","cta_url":"/catalogue/modules/","secondary_cta_label":"Back-office","secondary_cta_url":"/admin/login"}', 11
) AS defaults
CROSS JOIN (
    SELECT id
    FROM pages
    WHERE slug = 'home'
    LIMIT 1
) AS home
WHERE NOT EXISTS (
    SELECT 1
    FROM page_sections existing_sections
    WHERE existing_sections.page_id = home.id
);

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');
