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_data_categories/request"
	"go-humas-be/utils/paginator"
	"strings"
	"time"
)

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

// PpidDataCategoriesRepository define interface of IPpidDataCategoriesRepository
type PpidDataCategoriesRepository interface {
	GetAll(req request.PpidDataCategoriesQueryRequest) (ppidDataCategoriess []*entity.PpidDataCategories, paging paginator.Pagination, err error)
	GetAllNonPage(req request.PpidDataCategoriesQueryRequest) (ppidDataCategoriess []*entity.PpidDataCategories, paging paginator.Pagination, err error)
	FindOne(id uint) (ppidDataCategories *entity.PpidDataCategories, err error)
	FindOneBySlug(slug string) (ppidDataCategories *entity.PpidDataCategories, err error)
	Create(ppidDataCategories *entity.PpidDataCategories) (err error)
	Update(id uint, ppidDataCategories *entity.PpidDataCategories) (err error)
	Delete(id uint) (err error)
}

func NewPpidDataCategoriesRepository(db *database.Database, logger zerolog.Logger) PpidDataCategoriesRepository {
	return &ppidDataCategoriesRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of IPpidDataCategoriesRepository
func (_i *ppidDataCategoriesRepository) GetAll(req request.PpidDataCategoriesQueryRequest) (ppidDataCategoriess []*entity.PpidDataCategories, paging paginator.Pagination, err error) {
	var count int64

	_i.Log.Info().Str("timestamp", time.Now().
		Format(time.RFC3339)).Str("Service:GetAll", "Interface:ppidDataCategoriesRepository").
		Interface("req", req).Msg("")

	query := _i.DB.DB.Model(&entity.PpidDataCategories{})
	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.ParentId != nil {
		query = query.Where("parent_id = ?", req.ParentId)
	}
	if req.IsOnlyTop != nil {
		if *req.IsOnlyTop == true {
			query = query.Where("parent_id IS NULL")
		}
	}
	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(&ppidDataCategoriess).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

// implement interface of IPpidDataCategoriesRepository
func (_i *ppidDataCategoriesRepository) GetAllNonPage(req request.PpidDataCategoriesQueryRequest) (ppidDataCategoriess []*entity.PpidDataCategories, paging paginator.Pagination, err error) {
	var count int64

	_i.Log.Info().Str("timestamp", time.Now().
		Format(time.RFC3339)).Str("Service:GetAll", "Interface:ppidDataCategoriesRepository").
		Interface("req", req).Msg("")

	query := _i.DB.DB.Model(&entity.PpidDataCategories{})
	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.ParentId != nil {
		query = query.Where("parent_id = ?", req.ParentId)
	}
	if req.IsOnlyTop != nil {
		if *req.IsOnlyTop == true {
			query = query.Where("parent_id IS NULL")
		}
	}
	query.Count(&count)

	err = query.Find(&ppidDataCategoriess).Error
	if err != nil {
		return
	}

	return
}

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

	return ppidDataCategories, nil
}

func (_i *ppidDataCategoriesRepository) FindOneBySlug(slug string) (ppidDataCategories *entity.PpidDataCategories, err error) {
	if err := _i.DB.DB.Where("slug = ?", slug).First(&ppidDataCategories).Error; err != nil {
		return nil, err
	}

	return ppidDataCategories, nil
}

func (_i *ppidDataCategoriesRepository) Create(ppidDataCategories *entity.PpidDataCategories) (err error) {
	return _i.DB.DB.Create(ppidDataCategories).Error
}

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

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