package repository

import (
	"fmt"
	"github.com/rs/zerolog"
	"go-humas-be/app/database"
	"go-humas-be/app/database/entity"
	"go-humas-be/app/module/users/request"
	"go-humas-be/utils/paginator"
	"strings"
)

type usersRepository struct {
	DB  *database.Database
	Log zerolog.Logger
}

// UsersRepository define interface of IUsersRepository
type UsersRepository interface {
	GetAll(req request.UsersQueryRequest) (userss []*entity.Users, paging paginator.Pagination, err error)
	FindOne(id uint) (users *entity.Users, err error)
	FindByKeycloakId(keycloakId string) (users *entity.Users, err error)
	Create(users *entity.Users) (err error)
	Update(id uint, users *entity.Users) (err error)
	Delete(id uint) (err error)
}

func NewUsersRepository(db *database.Database, log zerolog.Logger) UsersRepository {
	return &usersRepository{
		DB:  db,
		Log: log,
	}
}

// implement interface of IUsersRepository
func (_i *usersRepository) GetAll(req request.UsersQueryRequest) (userss []*entity.Users, paging paginator.Pagination, err error) {
	var count int64

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

	if req.Username != nil && *req.Username != "" {
		username := strings.ToLower(*req.Username)
		query = query.Where("LOWER(username) LIKE ?", "%"+strings.ToLower(username)+"%")
	}
	if req.Fullname != nil && *req.Fullname != "" {
		fullname := strings.ToLower(*req.Fullname)
		query = query.Where("LOWER(fullname) LIKE ?", "%"+strings.ToLower(fullname)+"%")
	}
	if req.Email != nil && *req.Email != "" {
		query = query.Where("email = ?", req.Email)
	}
	if req.PhoneNumber != nil && *req.PhoneNumber != "" {
		query = query.Where("phone_number = ?", req.PhoneNumber)
	}
	if req.IdentityNumber != nil && *req.IdentityNumber != "" {
		query = query.Where("identity_number = ?", req.IdentityNumber)
	}
	if req.UserRoleId != nil {
		query = query.Where("user_role_id = ?", req.UserRoleId)
	}
	if req.StatusId != nil {
		query = query.Where("status_id = ?", req.StatusId)
	}
	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(&userss).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *usersRepository) FindOne(id uint) (users *entity.Users, err error) {
	if err := _i.DB.DB.First(&users, id).Error; err != nil {
		return nil, err
	}

	return users, nil
}

func (_i *usersRepository) FindByKeycloakId(keycloakId string) (users *entity.Users, err error) {
	if err := _i.DB.DB.Where("keycloak_id = ?", keycloakId).First(&users).Error; err != nil {
		return nil, err
	}

	return users, nil
}

func (_i *usersRepository) Create(users *entity.Users) (err error) {
	return _i.DB.DB.Create(users).Error
}

func (_i *usersRepository) Update(id uint, users *entity.Users) (err error) {
	return _i.DB.DB.Model(&entity.Users{}).
		Where(&entity.Users{ID: id}).
		Updates(users).Error
}

func (_i *usersRepository) Delete(id uint) error {
	return _i.DB.DB.Delete(&entity.Users{}, id).Error
}
