package services

import (
	"errors"

	"gauth-central/internal/database"
	"gauth-central/internal/models"

	"gorm.io/gorm"
)

type MainMenuService struct{}

func NewMainMenuService() *MainMenuService {
	return &MainMenuService{}
}

// CreateMainMenu creates a new main menu entry.
func (s *MainMenuService) CreateMainMenu(home string, about string, services string, resume string, portfolio string, contact string, isActive bool) (*models.MainMenu, error) {
	item := models.MainMenu{
		Home:      home,
		About:     about,
		Services:  services,
		Resume:    resume,
		Portfolio: portfolio,
		Contact:   contact,
		IsActive:  isActive,
	}

	if err := database.DB.Create(&item).Error; err != nil {
		return nil, err
	}

	return s.GetMainMenuByID(item.ID.String())
}

// GetAllMainMenus retrieves all main menu entries. Use onlyActive to filter public data.
func (s *MainMenuService) GetAllMainMenus(onlyActive bool) ([]models.MainMenu, error) {
	var items []models.MainMenu
	query := database.DB.Order("created_at desc")
	if onlyActive {
		query = query.Where("is_active = ?", true)
	}

	if err := query.Find(&items).Error; err != nil {
		return nil, err
	}

	return items, nil
}

// GetFirstActiveMainMenu returns the newest active main menu entry.
func (s *MainMenuService) GetFirstActiveMainMenu() (*models.MainMenu, error) {
	var item models.MainMenu
	if err := database.DB.Where("is_active = ?", true).Order("created_at desc").First(&item).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("main menu not found")
		}
		return nil, err
	}
	return &item, nil
}

// GetMainMenuByID retrieves a main menu entry by ID.
func (s *MainMenuService) GetMainMenuByID(id string) (*models.MainMenu, error) {
	var item models.MainMenu
	if err := database.DB.Where("id = ?", id).First(&item).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, errors.New("main menu not found")
		}
		return nil, err
	}
	return &item, nil
}

// UpdateMainMenu updates an existing main menu entry.
func (s *MainMenuService) UpdateMainMenu(
	id string,
	home *string,
	about *string,
	services *string,
	resume *string,
	portfolio *string,
	contact *string,
	isActive *bool,
) (*models.MainMenu, error) {
	item, err := s.GetMainMenuByID(id)
	if err != nil {
		return nil, err
	}

	updates := map[string]interface{}{}
	if home != nil {
		updates["home"] = *home
	}
	if about != nil {
		updates["about"] = *about
	}
	if services != nil {
		updates["services"] = *services
	}
	if resume != nil {
		updates["resume"] = *resume
	}
	if portfolio != nil {
		updates["portfolio"] = *portfolio
	}
	if contact != nil {
		updates["contact"] = *contact
	}
	if isActive != nil {
		updates["is_active"] = *isActive
	}

	if len(updates) > 0 {
		if err := database.DB.Model(item).Updates(updates).Error; err != nil {
			return nil, err
		}
	}

	return s.GetMainMenuByID(id)
}

// DeleteMainMenu deletes a main menu entry by ID.
func (s *MainMenuService) DeleteMainMenu(id string) error {
	result := database.DB.Delete(&models.MainMenu{}, "id = ?", id)
	if result.Error != nil {
		return result.Error
	}
	if result.RowsAffected == 0 {
		return errors.New("main menu not found")
	}
	return nil
}
