package repository

import (
	"netidhub-saas-be/app/database"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/user_role_level_details/request"
	"netidhub-saas-be/utils/paginator"
)

type userRoleLevelDetailsRepository struct {
	DB *database.Database
}

// UserRoleLevelDetailsRepository define interface of IUserRoleLevelDetailsRepository
type UserRoleLevelDetailsRepository interface {
	GetAll(req request.UserRoleLevelDetailsQueryRequest) (userRoleLevelDetailss []*entity.UserRoleLevelDetails, paging paginator.Pagination, err error)
	FindOne(id uint) (userRoleLevelDetails *entity.UserRoleLevelDetails, err error)
	FindByUserLevels(userLevelId uint) (userRoleLevelDetails []*entity.UserRoleLevelDetails, err error)
	Create(userRoleLevelDetails *entity.UserRoleLevelDetails) (err error)
	CreateAll(userRoleLevelDetails *[]entity.UserRoleLevelDetails) (err error)
	Update(id uint, userRoleLevelDetails *entity.UserRoleLevelDetails) (err error)
	Delete(id uint) (err error)
}

func NewUserRoleLevelDetailsRepository(db *database.Database) UserRoleLevelDetailsRepository {
	return &userRoleLevelDetailsRepository{
		DB: db,
	}
}

// implement interface of IUserRoleLevelDetailsRepository
func (_i *userRoleLevelDetailsRepository) GetAll(req request.UserRoleLevelDetailsQueryRequest) (userRoleLevelDetailss []*entity.UserRoleLevelDetails, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.UserRoleLevelDetails{})
	query.Count(&count)

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

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

	paging = *req.Pagination

	return
}

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

	return userRoleLevelDetails, nil
}

func (_i *userRoleLevelDetailsRepository) FindByUserLevels(userLevelId uint) (userRoleLevelDetails []*entity.UserRoleLevelDetails, err error) {
	if err := _i.DB.DB.Where(&entity.UserRoleLevelDetails{UserLevelId: userLevelId}).Find(&userRoleLevelDetails).Error; err != nil {
		return nil, err
	}

	return userRoleLevelDetails, nil
}

func (_i *userRoleLevelDetailsRepository) Create(userRoleLevelDetails *entity.UserRoleLevelDetails) (err error) {
	return _i.DB.DB.Create(userRoleLevelDetails).Error
}

func (_i *userRoleLevelDetailsRepository) CreateAll(userRoleLevelDetails *[]entity.UserRoleLevelDetails) (err error) {
	return _i.DB.DB.Create(userRoleLevelDetails).Error
}

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

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