package repository

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

type masterMenusRepository struct {
	DB *database.Database
}

// MasterMenusRepository define interface of IMasterMenusRepository
type MasterMenusRepository interface {
	GetAll(req request.MasterMenusQueryRequest) (masterMenuss []*entity.MasterMenus, paging paginator.Pagination, err error)
	FindOne(id uint) (masterMenus *entity.MasterMenus, err error)
	FindLastMenuPosition() (position *int, err error)
	Create(masterMenus *entity.MasterMenus) (err error)
	Update(id uint, masterMenus *entity.MasterMenus) (err error)
	Delete(id uint) (err error)
}

func NewMasterMenusRepository(db *database.Database) MasterMenusRepository {
	return &masterMenusRepository{
		DB: db,
	}
}

// implement interface of IMasterMenusRepository
func (_i *masterMenusRepository) GetAll(req request.MasterMenusQueryRequest) (masterMenuss []*entity.MasterMenus, paging paginator.Pagination, err error) {
	var count int64

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

	if req.Name != nil && *req.Name != "" {
		name := strings.ToLower(*req.Name)
		query = query.Where("LOWER(name) LIKE ?", "%"+strings.ToLower(name)+"%")
	}
	if req.Description != nil && *req.Description != "" {
		description := strings.ToLower(*req.Description)
		query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%")
	}
	if req.ModuleId != nil {
		query = query.Where("module_id = ?", req.ModuleId)
	}
	if req.ParentMenuId != nil {
		query = query.Where("parent_menu_id = ?", req.ParentMenuId)
	}
	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(&masterMenuss).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

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

	return masterMenus, nil
}

func (_i *masterMenusRepository) FindLastMenuPosition() (position *int, err error) {
	var masterMenus *entity.MasterMenus
	if err := _i.DB.DB.Where("position IS NOT NULL").Order(fmt.Sprintf("%s %s", "position", "DESC")).First(&masterMenus).Error; err != nil {
		return nil, err
	}

	return masterMenus.Position, nil
}

func (_i *masterMenusRepository) Create(masterMenus *entity.MasterMenus) (err error) {
	return _i.DB.DB.Create(masterMenus).Error
}

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

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