package controllers

import (
	"fotbal-club/internal/models"
	"fotbal-club/pkg/logger"
)

// seedDefaultHomePageElements creates default page element configurations for the homepage
// with all major sections visible by default (videos, gallery, matches, news, etc.)
func (bc *BaseController) seedDefaultHomePageElements() {
	// Check if any homepage elements already exist
	var count int64
	if err := bc.DB.Model(&models.PageElementConfig{}).Where("page_type = ?", "homepage").Count(&count).Error; err != nil {
		logger.Error("Failed to check existing page elements: %v", err)
		return
	}

	if count > 0 {
		logger.Info("Homepage page elements already exist, skipping seed")
		return
	}

	// Define default homepage elements with all major sections visible
	defaultElements := []models.PageElementConfig{
		// Hero section - main featured content
		{PageType: "homepage", ElementName: "hero", Variant: "grid", Visible: true, DisplayOrder: 1},
		
		// News/Articles - latest articles
		{PageType: "homepage", ElementName: "news", Variant: "grid", Visible: true, DisplayOrder: 2},
		
		// Matches - upcoming and recent matches
		{PageType: "homepage", ElementName: "matches", Variant: "compact", Visible: true, DisplayOrder: 3},
		
		// Table - league standings
		{PageType: "homepage", ElementName: "table", Variant: "split_news", Visible: true, DisplayOrder: 4},
		
		// Videos - YouTube videos and highlights
		{PageType: "homepage", ElementName: "videos", Variant: "grid", Visible: true, DisplayOrder: 5},
		
		// Gallery - photo gallery
		{PageType: "homepage", ElementName: "gallery", Variant: "grid", Visible: true, DisplayOrder: 6},
		
		// Team - players and squad
		{PageType: "homepage", ElementName: "team", Variant: "grid", Visible: true, DisplayOrder: 7},
		
		// Activities - upcoming events
		{PageType: "homepage", ElementName: "activities", Variant: "list", Visible: true, DisplayOrder: 8},
		
		// Sponsors - partners and sponsors
		{PageType: "homepage", ElementName: "sponsors", Variant: "grid", Visible: true, DisplayOrder: 9},
		
		// Newsletter - newsletter signup
		{PageType: "homepage", ElementName: "newsletter", Variant: "default", Visible: true, DisplayOrder: 10},
		
		// Contact - contact information and map
		{PageType: "homepage", ElementName: "contact", Variant: "combined", Visible: true, DisplayOrder: 11},
	}

	// Insert all default elements in a single transaction
	tx := bc.DB.Begin()
	for _, element := range defaultElements {
		if err := tx.Create(&element).Error; err != nil {
			logger.Error("Failed to create page element %s: %v", element.ElementName, err)
			tx.Rollback()
			return
		}
	}

	if err := tx.Commit().Error; err != nil {
		logger.Error("Failed to commit page elements transaction: %v", err)
		return
	}

	logger.Info("Successfully seeded %d default homepage page elements", len(defaultElements))
}
