package repository

import (
	"campaign-pool-be/app/database"
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/article_categories/request"
	"campaign-pool-be/utils/paginator"
	utilSvc "campaign-pool-be/utils/service"
	"fmt"
	"strings"

	"github.com/rs/zerolog"
)

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

// 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)
	FindOneByOldId(id uint) (articleCategories *entity.ArticleCategories, err error)
	FindOneBySlug(slug string) (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, log zerolog.Logger) ArticleCategoriesRepository {
	return &articleCategoriesRepository{
		DB:  db,
		Log: log,
	}
}

// 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{})

	if req.UserLevelId != nil {
		query = _i.DB.DB.Model(&entity.ArticleCategories{}).
			Joins("LEFT JOIN users ON article_categories.created_by_id = users.id").
			Joins("LEFT JOIN user_levels ON users.user_level_id = user_levels.id").
			Where("user_levels.id = ? or user_levels.parent_level_id = ?", *req.UserLevelId, *req.UserLevelId)
	}

	query = query.Where("article_categories.is_active = ?", true)

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

	if err := query.First(&articleCategories).Error; err != nil {
		return nil, err
	}

	return articleCategories, nil
}

func (_i *articleCategoriesRepository) FindOneByOldId(id uint) (articleCategories *entity.ArticleCategories, err error) {
	query := _i.DB.DB.Where("old_category_id = ?", id)

	if err := query.First(&articleCategories).Error; err != nil {
		return nil, err
	}

	return articleCategories, nil
}

func (_i *articleCategoriesRepository) FindOneBySlug(slug string) (articleCategories *entity.ArticleCategories, err error) {
	query := _i.DB.DB.Where("slug = ?", slug)

	if err := query.First(&articleCategories).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) {
	articleCategoriesMap, err := utilSvc.StructToMap(articleCategories)
	if err != nil {
		return err
	}
	query := _i.DB.DB.Model(&entity.ArticleCategories{}).Where(&entity.ArticleCategories{ID: id})
	return query.Updates(articleCategoriesMap).Error
}

func (_i *articleCategoriesRepository) Delete(id uint) error {
	query := _i.DB.DB.Model(&entity.ArticleCategories{}).Where("id = ?", id)
	return query.Delete(&entity.ArticleCategories{}).Error
}
