package repository

import (
	"fmt"
	"go-humas-be/app/database"
	"go-humas-be/app/database/entity"
	"go-humas-be/app/module/article_categories/request"
	"go-humas-be/utils/paginator"
	"strings"
)

type articleCategoriesRepository struct {
	DB *database.Database
}

// ArticleCategoriesRepository define interface of IArticleCategoriesRepository
type ArticleCategoriesRepository interface {
	GetAll(req request.ArticleCategoriesQueryRequest) (articleCategoriess []*entity.ArticleCategories, paging paginator.Pagination, err error)
	FindOne(id uint) (articleCategories *entity.ArticleCategories, err error)
	Create(articleCategories *entity.ArticleCategories) (articleCategoriesReturn *entity.ArticleCategories, err error)
	Update(id uint, articleCategories *entity.ArticleCategories) (err error)
	Delete(id uint) (err error)
}

func NewArticleCategoriesRepository(db *database.Database) ArticleCategoriesRepository {
	return &articleCategoriesRepository{
		DB: db,
	}
}

// implement interface of IArticleCategoriesRepository
func (_i *articleCategoriesRepository) GetAll(req request.ArticleCategoriesQueryRequest) (articleCategoriess []*entity.ArticleCategories, paging paginator.Pagination, err error) {
	var count int64

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

	if req.Title != nil && *req.Title != "" {
		title := strings.ToLower(*req.Title)
		query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
	}
	if req.Description != nil && *req.Description != "" {
		description := strings.ToLower(*req.Description)
		query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%")
	}
	if req.ParentId != nil {
		query = query.Where("parent_id = ?", req.ParentId)
	}
	if req.IsPublish != nil {
		query = query.Where("is_publish = ?", req.IsPublish)
	}
	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(&articleCategoriess).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return articleCategories, nil
}

func (_i *articleCategoriesRepository) Create(articleCategories *entity.ArticleCategories) (articleCategoriesReturn *entity.ArticleCategories, err error) {
	result := _i.DB.DB.Create(articleCategories)
	return articleCategories, result.Error
}

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

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