package repository

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

type articleFilesRepository struct {
	DB *database.Database
}

// ArticleFilesRepository define interface of IArticleFilesRepository
type ArticleFilesRepository interface {
	GetAll(req request.ArticleFilesQueryRequest) (articleFiless []*entity.ArticleFiles, paging paginator.Pagination, err error)
	FindOne(id uint) (articleFiles *entity.ArticleFiles, err error)
	FindByArticle(articleId uint) (articleFiles []*entity.ArticleFiles, err error)
	FindByFilename(filename string) (articleFiles *entity.ArticleFiles, err error)
	Create(articleFiles *entity.ArticleFiles) (err error)
	Update(id uint, articleFiles *entity.ArticleFiles) (err error)
	Delete(id uint) (err error)
}

func NewArticleFilesRepository(db *database.Database) ArticleFilesRepository {
	return &articleFilesRepository{
		DB: db,
	}
}

// implement interface of IArticleFilesRepository
func (_i *articleFilesRepository) GetAll(req request.ArticleFilesQueryRequest) (articleFiless []*entity.ArticleFiles, paging paginator.Pagination, err error) {
	var count int64

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

	if req.ArticleId != nil {
		query = query.Where("article_id = ?", req.ArticleId)
	}
	if req.FileName != nil && *req.FileName != "" {
		fileName := strings.ToLower(*req.FileName)
		query = query.Where("LOWER(file_name) LIKE ?", "%"+strings.ToLower(fileName)+"%")
	}
	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(&articleFiless).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *articleFilesRepository) FindOne(id uint) (articleFiles *entity.ArticleFiles, err error) {
	query := _i.DB.DB

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

	return articleFiles, nil
}

func (_i *articleFilesRepository) FindByArticle(articleId uint) (articleFiles []*entity.ArticleFiles, err error) {
	query := _i.DB.DB.Where("article_id = ?", articleId)

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

	return articleFiles, nil
}

func (_i *articleFilesRepository) FindByFilename(articleFilename string) (articleFiles *entity.ArticleFiles, err error) {
	query := _i.DB.DB.Where("file_name = ?", articleFilename)

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

	return articleFiles, nil
}

func (_i *articleFilesRepository) Create(articleFiles *entity.ArticleFiles) (err error) {
	return _i.DB.DB.Create(articleFiles).Error
}

func (_i *articleFilesRepository) Update(id uint, articleFiles *entity.ArticleFiles) (err error) {
	articleFilesMap, err := utilSvc.StructToMap(articleFiles)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&entity.ArticleFiles{}).
		Where(&entity.ArticleFiles{ID: id}).
		Updates(articleFilesMap).Error
}

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