package repository

import (
	"fmt"
	"narasi-ahli-be/app/database"
	"narasi-ahli-be/app/database/entity"
	"narasi-ahli-be/app/module/article_approvals/request"
	"narasi-ahli-be/utils/paginator"
	utilSvc "narasi-ahli-be/utils/service"
	"strings"

	"github.com/rs/zerolog"
)

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

// ArticleApprovalsRepository define interface of IArticleApprovalsRepository
type ArticleApprovalsRepository interface {
	GetAll(req request.ArticleApprovalsQueryRequest) (articleApprovalss []*entity.ArticleApprovals, paging paginator.Pagination, err error)
	FindOne(id uint) (articleApprovals *entity.ArticleApprovals, err error)
	Create(articleApprovals *entity.ArticleApprovals) (articleApprovalsReturn *entity.ArticleApprovals, err error)
	Update(id uint, articleApprovals *entity.ArticleApprovals) (err error)
	Delete(id uint) (err error)
}

func NewArticleApprovalsRepository(db *database.Database, logger zerolog.Logger) ArticleApprovalsRepository {
	return &articleApprovalsRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of IArticleApprovalsRepository
func (_i *articleApprovalsRepository) GetAll(req request.ArticleApprovalsQueryRequest) (articleApprovalss []*entity.ArticleApprovals, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.ArticleApprovals{})
	if req.ArticleId != nil {
		query = query.Where("article_id = ?", req.ArticleId)
	}
	if req.ApprovalBy != nil {
		query = query.Where("approval_by = ?", req.ApprovalBy)
	}
	if req.StatusId != nil {
		query = query.Where("status_id = ?", req.StatusId)
	}
	if req.Message != nil && *req.Message != "" {
		message := strings.ToLower(*req.Message)
		query = query.Where("LOWER(message) LIKE ?", "%"+strings.ToLower(message)+"%")
	}
	if req.ApprovalAtLevel != nil {
		query = query.Where("approval_at_level = ?", req.ApprovalAtLevel)
	}
	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(&articleApprovalss).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return articleApprovals, nil
}

func (_i *articleApprovalsRepository) Create(articleApprovals *entity.ArticleApprovals) (articleApprovalsReturn *entity.ArticleApprovals, err error) {
	result := _i.DB.DB.Create(articleApprovals)
	return articleApprovals, result.Error
}

func (_i *articleApprovalsRepository) Update(id uint, articleApprovals *entity.ArticleApprovals) (err error) {
	articleApprovalsMap, err := utilSvc.StructToMap(articleApprovals)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&entity.ArticleApprovals{}).
		Where(&entity.ArticleApprovals{ID: id}).
		Updates(articleApprovalsMap).Error
}

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