package request

import (
	"go-humas-be/app/database/entity"
	"go-humas-be/utils/paginator"
	"strconv"
	"time"
)

type MasterMenusGeneric interface {
	ToEntity()
}

type MasterMenusQueryRequest struct {
	Name         *string               `json:"name"`
	Description  *string               `json:"description"`
	ModuleId     *int                  `json:"moduleId"`
	ParentMenuId *int                  `json:"parentMenuId"`
	StatusId     *int                  `json:"statusId"`
	Pagination   *paginator.Pagination `json:"pagination"`
}

type MasterMenusCreateRequest struct {
	Name         string `json:"name" validate:"required"`
	Description  string `json:"description" validate:"required"`
	ModuleId     int    `json:"moduleId" validate:"required"`
	ParentMenuId int    `json:"parentMenuId" validate:"required"`
	Icon         string `json:"icon" validate:"required"`
	StatusId     int    `json:"statusId" validate:"required"`
	Position     *int   `json:"position"`
}

func (req MasterMenusCreateRequest) ToEntity() *entity.MasterMenus {
	return &entity.MasterMenus{
		Name:         req.Name,
		Description:  req.Description,
		ModuleId:     req.ModuleId,
		ParentMenuId: req.ParentMenuId,
		Icon:         req.Icon,
		Position:     req.Position,
		StatusId:     req.StatusId,
	}
}

type MasterMenusUpdateRequest struct {
	ID           uint      `json:"id" validate:"required"`
	Name         string    `json:"name" validate:"required"`
	Description  string    `json:"description" validate:"required"`
	ModuleId     int       `json:"moduleId" validate:"required"`
	ParentMenuId int       `json:"parentMenuId" validate:"required"`
	Icon         string    `json:"icon" validate:"required"`
	StatusId     int       `json:"statusId" validate:"required"`
	Position     *int      `json:"position"`
	UpdatedAt    time.Time `json:"updatedAt"`
}

func (req MasterMenusUpdateRequest) ToEntity() *entity.MasterMenus {
	return &entity.MasterMenus{
		ID:           req.ID,
		Name:         req.Name,
		Description:  req.Description,
		ModuleId:     req.ModuleId,
		ParentMenuId: req.ParentMenuId,
		Icon:         req.Icon,
		Position:     req.Position,
		StatusId:     req.StatusId,
		UpdatedAt:    time.Now(),
	}
}

type MasterMenusQueryRequestContext struct {
	Name         string `json:"name"`
	Description  string `json:"description"`
	ModuleId     string `json:"moduleId"`
	ParentMenuId string `json:"parentMenuId"`
	StatusId     string `json:"statusId"`
}

func (req MasterMenusQueryRequestContext) ToParamRequest() MasterMenusQueryRequest {
	var request MasterMenusQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if moduleIdStr := req.ModuleId; moduleIdStr != "" {
		moduleId, err := strconv.Atoi(moduleIdStr)
		if err == nil {
			request.ModuleId = &moduleId
		}
	}
	if parentMenuIdStr := req.ParentMenuId; parentMenuIdStr != "" {
		parentMenuId, err := strconv.Atoi(parentMenuIdStr)
		if err == nil {
			request.ParentMenuId = &parentMenuId
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}

	return request
}
