package repository

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

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

// ArticlesRepository define interface of IArticlesRepository
type ArticlesRepository interface {
	GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error)
	FindOne(id uint) (articles *entity.Articles, err error)
	FindByFilename(thumbnailName string) (articleReturn *entity.Articles, err error)
	Create(articles *entity.Articles) (articleReturn *entity.Articles, err error)
	Update(id uint, articles *entity.Articles) (err error)
	Delete(id uint) (err error)
}

func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRepository {
	return &articlesRepository{
		DB:  db,
		Log: log,
	}
}

// implement interface of IArticlesRepository
func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.Articles{})
	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.Tags != nil && *req.Tags != "" {
		tags := strings.ToLower(*req.Tags)
		query = query.Where("LOWER(tags) LIKE ?", "%"+strings.ToLower(tags)+"%")
	}
	if req.CategoryId != nil {
		query = query.Where("category_id = ?", req.CategoryId)
	}
	if req.TypeId != nil {
		query = query.Where("type_id = ?", req.TypeId)
	}
	if req.IsPublish != nil {
		query = query.Where("is_publish = ?", req.IsPublish)
	}
	if req.StatusId != nil {
		query = query.Where("status_id = ?", req.StatusId)
	}
	if req.CreatedById != nil {
		query = query.Where("created_by_id = ?", req.CreatedById)
	}
	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(&articless).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return articles, nil
}

func (_i *articlesRepository) FindByFilename(thumbnailName string) (articles *entity.Articles, err error) {

	if err := _i.DB.DB.Where("thumbnail_name = ?", thumbnailName).First(&articles).Error; err != nil {
		return nil, err
	}

	return articles, nil
}

func (_i *articlesRepository) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) {
	result := _i.DB.DB.Create(articles)
	return articles, result.Error
}

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

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