package repository

import (
	"strings"
	"time"

	"hyapp-admin-server/internal/model"

	"gorm.io/gorm/clause"
)

type AppVersionListOptions struct {
	AppCode  string
	Platform string
	Keyword  string
}

type AppExploreTabListOptions struct {
	AppCode string
	Keyword string
	Enabled *bool
}

func (s *Store) ListAppConfigs(group string) ([]model.AppConfig, error) {
	var items []model.AppConfig
	err := s.db.Where("`group` = ?", strings.TrimSpace(group)).Order("`key` ASC").Find(&items).Error
	return items, err
}

func (s *Store) GetAppConfig(group string, key string) (model.AppConfig, error) {
	var item model.AppConfig
	err := s.db.Where("`group` = ? AND `key` = ?", strings.TrimSpace(group), strings.TrimSpace(key)).First(&item).Error
	return item, err
}

func (s *Store) CreateAppConfig(item *model.AppConfig) error {
	return s.db.Create(item).Error
}

func (s *Store) UpdateAppConfig(item *model.AppConfig) error {
	return s.db.Save(item).Error
}

func (s *Store) DeleteAppConfig(group string, key string) error {
	return s.db.Where("`group` = ? AND `key` = ?", strings.TrimSpace(group), strings.TrimSpace(key)).Delete(&model.AppConfig{}).Error
}

func (s *Store) UpsertAppConfigs(items []model.AppConfig) error {
	if len(items) == 0 {
		return nil
	}

	nowMS := time.Now().UTC().UnixMilli()
	for index := range items {
		items[index].Group = strings.TrimSpace(items[index].Group)
		items[index].Key = strings.TrimSpace(items[index].Key)
		items[index].UpdatedAtMS = nowMS
		if items[index].CreatedAtMS == 0 {
			items[index].CreatedAtMS = nowMS
		}
	}

	return s.db.Clauses(clause.OnConflict{
		Columns: []clause.Column{
			{Name: "group"},
			{Name: "key"},
		},
		DoUpdates: clause.AssignmentColumns([]string{"value", "description", "updated_at_ms"}),
	}).Create(&items).Error
}

func (s *Store) ListAppVersions(options AppVersionListOptions) ([]model.AppVersion, error) {
	db := s.db.Where("app_code = ?", strings.TrimSpace(options.AppCode))
	if platform := strings.TrimSpace(options.Platform); platform != "" {
		db = db.Where("platform = ?", platform)
	}
	if keyword := strings.TrimSpace(options.Keyword); keyword != "" {
		like := "%" + keyword + "%"
		db = db.Where("version LIKE ? OR download_url LIKE ? OR description LIKE ?", like, like, like)
	}

	var items []model.AppVersion
	err := db.Order("platform ASC, build_number DESC, id DESC").Find(&items).Error
	return items, err
}

func (s *Store) GetAppVersion(appCode string, id uint) (model.AppVersion, error) {
	var item model.AppVersion
	err := s.db.Where("app_code = ? AND id = ?", strings.TrimSpace(appCode), id).First(&item).Error
	return item, err
}

func (s *Store) CreateAppVersion(item *model.AppVersion) error {
	return s.db.Create(item).Error
}

func (s *Store) UpdateAppVersion(item *model.AppVersion) error {
	return s.db.Save(item).Error
}

func (s *Store) DeleteAppVersion(appCode string, id uint) error {
	return s.db.Where("app_code = ? AND id = ?", strings.TrimSpace(appCode), id).Delete(&model.AppVersion{}).Error
}

func (s *Store) ListAppExploreTabs(options AppExploreTabListOptions) ([]model.AppExploreTab, error) {
	db := s.db.Where("app_code = ?", strings.TrimSpace(options.AppCode))
	if options.Enabled != nil {
		db = db.Where("enabled = ?", *options.Enabled)
	}
	if keyword := strings.TrimSpace(options.Keyword); keyword != "" {
		like := "%" + keyword + "%"
		db = db.Where("tab LIKE ? OR h5_url LIKE ?", like, like)
	}

	var items []model.AppExploreTab
	err := db.Order("sort_order ASC, id DESC").Find(&items).Error
	return items, err
}

func (s *Store) GetAppExploreTab(appCode string, id uint) (model.AppExploreTab, error) {
	var item model.AppExploreTab
	err := s.db.Where("app_code = ? AND id = ?", strings.TrimSpace(appCode), id).First(&item).Error
	return item, err
}

func (s *Store) CreateAppExploreTab(item *model.AppExploreTab) error {
	return s.db.Create(item).Error
}

func (s *Store) UpdateAppExploreTab(item *model.AppExploreTab) error {
	return s.db.Save(item).Error
}

func (s *Store) DeleteAppExploreTab(appCode string, id uint) error {
	return s.db.Where("app_code = ? AND id = ?", strings.TrimSpace(appCode), id).Delete(&model.AppExploreTab{}).Error
}
