package services

import (
	"nearle/models"
	"nearle/repositories"
)

type UtilsService interface {
	GetApptypes(tag string) ([]models.Apptypes, error)
	SendNotification(token string, notification models.FcmNotification, data map[string]string) error
	GetSubcategories(moduleid int, categoryid int) ([]models.Appsubcategories, error)
	GetApplocations(aid int) ([]models.Applocations, error)
	GetApplocationConfig(aid int) ([]models.Applocations, error)
	GetAppConfig(configID int) (models.Appconfig, error)
	GetAppCategory() ([]models.AppCategory, error)
}

type utilsService struct {
	repo repositories.UtilsRepository
}

func NewUtilsService(repo repositories.UtilsRepository) UtilsService {
	return &utilsService{repo: repo}
}

func (s *utilsService) GetApptypes(tag string) ([]models.Apptypes, error) {
	return s.repo.GetApptypes(tag)
}

func (s *utilsService) SendNotification(token string, notification models.FcmNotification, data map[string]string) error {
	return s.repo.SendNotification(token, notification, data)
}

func (s *utilsService) GetSubcategories(moduleid int, categoryid int) ([]models.Appsubcategories, error) {
	return s.repo.GetSubcategories(moduleid, categoryid)
}

func (s *utilsService) GetApplocations(aid int) ([]models.Applocations, error) {
	return s.repo.GetApplocations(aid)
}

func (s *utilsService) GetApplocationConfig(aid int) ([]models.Applocations, error) {
	return s.repo.GetApplocationConfig(aid)
}

func (s *utilsService) GetAppConfig(configID int) (models.Appconfig, error) {
	return s.repo.GetAppConfig(configID)
}

func (s *utilsService) GetAppCategory() ([]models.AppCategory, error) {
	categories, err := s.repo.GetAppCategory()
	if err != nil {
		return nil, err
	}

	// "All" is a synthetic pseudo-category, not a real app_category row — it
	// must never be inserted into app_category itself, since product-listing
	// filters (e.g. ProductController.GetAllProducts) already treat
	// categoryid=0 as "no category filter". Keeping this row's id at 0 reuses
	// that existing convention instead of adding a new one.
	all := models.AppCategory{
		Categoryid:   0,
		Categoryname: "All",
		Categorytype: 7,
		Sortorder:    0,
		Crossaxis:    1,
		Mainaxis:     1,
		Status:       "Active",
	}

	return append([]models.AppCategory{all}, categories...), nil
}
