package repository

import (
	"fmt"
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"strings"
	"web-medols-be/app/database"
	"web-medols-be/app/database/entity"
	"web-medols-be/app/module/custom_static_pages/request"
	"web-medols-be/utils/paginator"
	utilSvc "web-medols-be/utils/service"
)

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

// CustomStaticPagesRepository define interface of ICustomStaticPagesRepository
type CustomStaticPagesRepository interface {
	GetAll(clientId *uuid.UUID, req request.CustomStaticPagesQueryRequest) (customStaticPagess []*entity.CustomStaticPages, paging paginator.Pagination, err error)
	FindOne(clientId *uuid.UUID, id uint) (customStaticPages *entity.CustomStaticPages, err error)
	FindOneBySlug(clientId *uuid.UUID, slug string) (customStaticPages *entity.CustomStaticPages, err error)
	Create(clientId *uuid.UUID, customStaticPages *entity.CustomStaticPages) (customStaticPagesReturn *entity.CustomStaticPages, err error)
	Update(clientId *uuid.UUID, id uint, customStaticPages *entity.CustomStaticPages) (err error)
	Delete(clientId *uuid.UUID, 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(clientId *uuid.UUID, 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 = ?", clientId)

	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(clientId *uuid.UUID, id uint) (customStaticPages *entity.CustomStaticPages, err error) {
	if err := _i.DB.DB.Where("client_id = ?", clientId).First(&customStaticPages, id).Error; err != nil {
		return nil, err
	}

	return customStaticPages, nil
}

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

	return customStaticPages, nil
}

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

func (_i *customStaticPagesRepository) Update(clientId *uuid.UUID, 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, ClientId: clientId}).
		Updates(customStaticPagesMap).Error
}

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