package repository

import (
	"fmt"
	"github.com/rs/zerolog"
	"strings"
	"web-qudo-be/app/database"
	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/article_nulis_ai/request"
	"web-qudo-be/utils/paginator"
)

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

// ArticleNulisAIRepository define interface of IArticleNulisAIRepository
type ArticleNulisAIRepository interface {
	GetAll(req request.ArticleNulisAIQueryRequest) (articleNulisAIs []*entity.ArticleNulisAI, paging paginator.Pagination, err error)
	FindOne(id uint) (articleNulisAI *entity.ArticleNulisAI, err error)
	Create(articleNulisAI *entity.ArticleNulisAI) (articleNulisAIReturn *entity.ArticleNulisAI, err error)
	Update(id uint, articleNulisAI *entity.ArticleNulisAI) (err error)
	Delete(id uint) (err error)
}

func NewArticleNulisAIRepository(db *database.Database, logger zerolog.Logger) ArticleNulisAIRepository {
	return &articleNulisAIRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of IArticleNulisAIRepository
func (_i *articleNulisAIRepository) GetAll(req request.ArticleNulisAIQueryRequest) (articleNulisAIs []*entity.ArticleNulisAI, paging paginator.Pagination, err error) {
	var count int64

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

	if req.NulisAiId != nil {
		query = query.Where("nulis_ai_id = ?", req.NulisAiId)
	}
	if req.ArticleId != nil {
		query = query.Where("article_id = ?", req.ArticleId)
	}
	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.HtmlDescription != nil && *req.HtmlDescription != "" {
		htmlDescription := strings.ToLower(*req.HtmlDescription)
		query = query.Where("LOWER(html_description) LIKE ?", "%"+strings.ToLower(htmlDescription)+"%")
	}
	if req.CategoryId != nil {
		query = query.Where("category_id = ?", req.CategoryId)
	}
	if req.CreatorId != nil {
		query = query.Where("creator_id = ?", req.CreatorId)
	}
	if req.Tags != nil && *req.Tags != "" {
		tags := strings.ToLower(*req.Tags)
		query = query.Where("LOWER(tags) LIKE ?", "%"+strings.ToLower(tags)+"%")
	}
	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(&articleNulisAIs).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return articleNulisAI, nil
}

func (_i *articleNulisAIRepository) Create(articleNulisAI *entity.ArticleNulisAI) (articleNulisAIReturn *entity.ArticleNulisAI, err error) {
	result := _i.DB.DB.Create(articleNulisAI)
	return articleNulisAI, result.Error
}

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

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