package repository

import (
	"web-qudo-be/app/database"
	"web-qudo-be/app/database/entity/article_category_details"
	"web-qudo-be/app/module/article_category_details/request"
	"web-qudo-be/utils/paginator"
)

type articleCategoryDetailsRepository struct {
	DB *database.Database
}

// ArticleCategoryDetailsRepository define interface of IArticleCategoryDetailsRepository
type ArticleCategoryDetailsRepository interface {
	GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, paging paginator.Pagination, err error)
	FindOne(id uint) (articleCategoryDetails *article_category_details.ArticleCategoryDetails, err error)
	FindByArticleId(articleId uint) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, err error)
	Create(articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error)
	Update(id uint, articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error)
	Delete(id uint) (err error)
}

func NewArticleCategoryDetailsRepository(db *database.Database) ArticleCategoryDetailsRepository {
	return &articleCategoryDetailsRepository{
		DB: db,
	}
}

// implement interface of IArticleCategoryDetailsRepository
func (_i *articleCategoryDetailsRepository) GetAll(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&article_category_details.ArticleCategoryDetails{})
	query.Count(&count)

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

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

	paging = *req.Pagination

	return
}

func (_i *articleCategoryDetailsRepository) FindOne(id uint) (articleCategoryDetails *article_category_details.ArticleCategoryDetails, err error) {
	if err := _i.DB.DB.First(&articleCategoryDetails, id).Error; err != nil {
		return nil, err
	}

	return articleCategoryDetails, nil
}

func (_i *articleCategoryDetailsRepository) FindByArticleId(articleId uint) (articleCategoryDetailss []*article_category_details.ArticleCategoryDetails, err error) {
	if err := _i.DB.DB.Where("article_id = ?", articleId).Preload("Category").Find(&articleCategoryDetailss).Error; err != nil {
		return nil, err
	}

	return articleCategoryDetailss, nil
}

func (_i *articleCategoryDetailsRepository) Create(articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error) {
	return _i.DB.DB.Create(articleCategoryDetails).Error
}

func (_i *articleCategoryDetailsRepository) Update(id uint, articleCategoryDetails *article_category_details.ArticleCategoryDetails) (err error) {
	return _i.DB.DB.Model(&article_category_details.ArticleCategoryDetails{}).
		Where(&article_category_details.ArticleCategoryDetails{ID: id}).
		Updates(articleCategoryDetails).Error
}

func (_i *articleCategoryDetailsRepository) Delete(id uint) error {
	return _i.DB.DB.Delete(&article_category_details.ArticleCategoryDetails{}, id).Error
}
