-- Seed default layout, layout sections, and homepage widgets.
--
-- Migration 026 and earlier migrations attempted to seed layout_sections and
-- widgets for published layouts, but because layouts was empty on fresh
-- installations, those SELECT ... FROM layouts queries inserted zero rows.
-- This migration seeds the default published layout, the 9 layout sections,
-- and the default homepage widgets.

-- 1. Ensure at least one published layout exists
INSERT INTO layouts (name, slug, description, status, created_at, updated_at)
SELECT 'Default', 'default', 'Default layout', 'published', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM layouts);

-- 2. Seed the 9 layout sections for all published layouts
INSERT INTO layout_sections (layout_id, section_key, name, sort_order, created_at, updated_at)
SELECT l.id, seeded.section_key, seeded.name, seeded.sort_order, NOW(), NOW()
FROM layouts l
INNER JOIN (
    SELECT 'homepage_main' AS section_key, 'Homepage main' AS name, 5 AS sort_order
    UNION ALL SELECT 'header_top', 'Header top', 10
    UNION ALL SELECT 'nav_extra', 'Navigation extra', 20
    UNION ALL SELECT 'home_sidebar', 'Home sidebar', 30
    UNION ALL SELECT 'question_sidebar', 'Question sidebar', 40
    UNION ALL SELECT 'footer_col_1', 'Footer column 1', 50
    UNION ALL SELECT 'footer_col_2', 'Footer column 2', 60
    UNION ALL SELECT 'footer_col_3', 'Footer column 3', 70
    UNION ALL SELECT 'footer_bottom', 'Footer bottom', 80
) AS seeded
WHERE l.status = 'published'
  AND NOT EXISTS (
      SELECT 1 FROM layout_sections existing
      WHERE existing.layout_id = l.id AND existing.section_key = seeded.section_key
  );

-- 3. Seed default widgets into homepage_main for all published layouts
INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'ad_slot', NULL, '{"placement":"homepage"}', 190, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'ad_slot');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'top_contributors', NULL, '{"limit":5}', 200, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'top_contributors');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'most_viewed', NULL, '{"limit":5}', 210, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'most_viewed');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'most_useful', NULL, '{"limit":5}', 220, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'most_useful');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'most_answered', NULL, '{"limit":5}', 230, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'most_answered');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'unanswered', NULL, '{"limit":5}', 240, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'unanswered');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'active_tags', NULL, '{"limit":10}', 250, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'active_tags');

INSERT INTO widgets (layout_section_id, type, title, config_json, sort_order, is_enabled, created_at, updated_at)
SELECT ls.id, 'trending', NULL, '{"limit":5}', 260, 1, NOW(), NOW()
FROM layout_sections ls INNER JOIN layouts l ON l.id = ls.layout_id
WHERE l.status = 'published' AND ls.section_key = 'homepage_main'
  AND NOT EXISTS (SELECT 1 FROM widgets w WHERE w.layout_section_id = ls.id AND w.type = 'trending');
