package repository

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

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

// PpidDatasRepository define interface of IPpidDatasRepository
type PpidDatasRepository interface {
	GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error)
	FindOne(id uint) (ppidDatas *entity.PpidDatas, err error)
	FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error)
	Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error)
	Update(id uint, ppidDatas *entity.PpidDatas) (err error)
	Delete(id uint) (err error)
}

func NewPpidDatasRepository(db *database.Database, logger zerolog.Logger) PpidDatasRepository {
	return &ppidDatasRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of IPpidDatasRepository
func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.PpidDatas{})
	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.CategoryId != nil {
		query = query.Where("category_id = ?", req.CategoryId)
	}
	if req.Group != nil && *req.Group != "" {
		title := strings.ToLower(*req.Title)
		query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
	}
	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(&ppidDatass).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return ppidDatas, nil
}

func (_i *ppidDatasRepository) FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error) {
	if err := _i.DB.DB.Where("slug = ?", slug).First(&ppidDatas).Error; err != nil {
		return nil, err
	}

	return ppidDatas, nil
}

func (_i *ppidDatasRepository) Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error) {
	result := _i.DB.DB.Create(ppidDatas)

	_i.Log.Info().Str("timestamp", time.Now().
		Format(time.RFC3339)).Str("Service:Create", "PpidData:Create").
		Interface("result", ppidDatas).Msg("")

	return ppidDatas, result.Error
}

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

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