package repository

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

	"github.com/rs/zerolog"
)

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

// CustomStaticPagesRepository define interface of ICustomStaticPagesRepository
type CustomStaticPagesRepository interface {
	GetAll(req request.CustomStaticPagesQueryRequest) (customStaticPagess []*entity.CustomStaticPages, paging paginator.Pagination, err error)
	FindOne(id uint) (customStaticPages *entity.CustomStaticPages, err error)
	FindOneBySlug(slug string) (customStaticPages *entity.CustomStaticPages, err error)
	Create(customStaticPages *entity.CustomStaticPages) (customStaticPagesReturn *entity.CustomStaticPages, err error)
	Update(id uint, customStaticPages *entity.CustomStaticPages) (err error)
	Delete(id uint) (err error)
}

func NewCustomStaticPagesRepository(db *database.Database, logger zerolog.Logger) CustomStaticPagesRepository {
	return &customStaticPagesRepository{
		DB:  db,
		Log: logger,
	}
}

// implement interface of ICustomStaticPagesRepository
func (_i *customStaticPagesRepository) GetAll(req request.CustomStaticPagesQueryRequest) (customStaticPagess []*entity.CustomStaticPages, paging paginator.Pagination, err error) {
	var count int64

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

	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.Slug != nil && *req.Slug != "" {
		slug := strings.ToLower(*req.Slug)
		query = query.Where("LOWER(slug) LIKE ?", "%"+strings.ToLower(slug)+"%")
	}
	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(&customStaticPagess).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *customStaticPagesRepository) FindOne(id uint) (customStaticPages *entity.CustomStaticPages, err error) {
	if err := _i.DB.DB.Where("client_id = ?").First(&customStaticPages, id).Error; err != nil {
		return nil, err
	}

	return customStaticPages, nil
}

func (_i *customStaticPagesRepository) FindOneBySlug(slug string) (customStaticPages *entity.CustomStaticPages, err error) {
	if err := _i.DB.DB.Where("client_id = ? AND slug = ?", slug).First(&customStaticPages).Error; err != nil {
		return nil, err
	}

	return customStaticPages, nil
}

func (_i *customStaticPagesRepository) Create(customStaticPages *entity.CustomStaticPages) (customStaticPagesReturn *entity.CustomStaticPages, err error) {
	result := _i.DB.DB.Create(customStaticPages)
	return customStaticPages, result.Error
}

func (_i *customStaticPagesRepository) Update(id uint, customStaticPages *entity.CustomStaticPages) (err error) {
	customStaticPagesMap, err := utilSvc.StructToMap(customStaticPages)
	if err != nil {
		return err
	}
	return _i.DB.DB.Model(&entity.CustomStaticPages{}).
		Where(&entity.CustomStaticPages{ID: id}).
		Updates(customStaticPagesMap).Error
}

func (_i *customStaticPagesRepository) Delete(id uint) error {
	return _i.DB.DB.Where("client_id = ?").Delete(&entity.CustomStaticPages{}, id).Error
}
