package repository

import (
	"campaign-pool-be/app/database"
	"campaign-pool-be/app/database/entity/user_levels"
	"campaign-pool-be/app/module/user_levels/request"
	"campaign-pool-be/utils/paginator"
	utilSvc "campaign-pool-be/utils/service"
	"fmt"
	"strings"
)

type userLevelsRepository struct {
	DB *database.Database
}

// UserLevelsRepository define interface of IUserLevelsRepository
type UserLevelsRepository interface {
	GetAll(req request.UserLevelsQueryRequest) (userLevelss []*user_levels.UserLevels, paging paginator.Pagination, err error)
	FindOne(id uint) (userLevels *user_levels.UserLevels, err error)
	FindOneByAlias(alias string) (userLevels *user_levels.UserLevels, err error)
	Create(userLevels *user_levels.UserLevels) (userLevelsReturn *user_levels.UserLevels, err error)
	Update(id uint, userLevels *user_levels.UserLevels) (err error)
	Delete(id uint) (err error)
}

func NewUserLevelsRepository(db *database.Database) UserLevelsRepository {
	return &userLevelsRepository{
		DB: db,
	}
}

// implement interface of IUserLevelsRepository
func (_i *userLevelsRepository) GetAll(req request.UserLevelsQueryRequest) (userLevelss []*user_levels.UserLevels, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&user_levels.UserLevels{})
	query = query.Where("is_active = ?", true)

	if req.Name != nil && *req.Name != "" {
		name := strings.ToLower(*req.Name)
		query = query.Where("LOWER(name) LIKE ?", "%"+strings.ToLower(name)+"%")
	}
	if req.LevelNumber != nil {
		query = query.Where("level_number = ?", req.LevelNumber)
	}
	if req.ParentLevelId != nil {
		query = query.Where("parent_level_id = ?", req.ParentLevelId)
	}
	if req.ProvinceId != nil {
		query = query.Where("province_id = ?", req.ProvinceId)
	}
	query.Count(&count)

	if req.Pagination.SortBy != "" {
		direction := "ASC"
		if req.Pagination.Sort == "desc" {
			direction = "DESC"
		}
		query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
	}

	req.Pagination.Count = count
	req.Pagination = paginator.Paging(req.Pagination)

	err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&userLevelss).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *userLevelsRepository) FindOne(id uint) (userLevels *user_levels.UserLevels, err error) {
	if err := _i.DB.DB.First(&userLevels, id).Error; err != nil {
		return nil, err
	}

	return userLevels, nil
}

func (_i *userLevelsRepository) FindOneByAlias(alias string) (userLevels *user_levels.UserLevels, err error) {
	if err := _i.DB.DB.Where("alias_name = ?", strings.ToLower(alias)).First(&userLevels).Error; err != nil {
		return nil, err
	}

	return userLevels, nil
}

func (_i *userLevelsRepository) Create(userLevels *user_levels.UserLevels) (userLevelsReturn *user_levels.UserLevels, err error) {
	result := _i.DB.DB.Create(userLevels)
	return userLevels, result.Error
}

func (_i *userLevelsRepository) Update(id uint, userLevels *user_levels.UserLevels) (err error) {
	userLevelsMap, err := utilSvc.StructToMap(userLevels)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&user_levels.UserLevels{}).
		Where(&user_levels.UserLevels{ID: id}).
		Updates(userLevelsMap).Error
}

func (_i *userLevelsRepository) Delete(id uint) error {
	return _i.DB.DB.Delete(&user_levels.UserLevels{}, id).Error
}
