package request

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

type UserRoleAccessesGeneric interface {
	ToEntity()
}

type UserRoleAccessesQueryRequest struct {
	UserRoleId *int                  `json:"userRoleId" validate:"required"`
	MenuId     *int                  `json:"menuId" validate:"required"`
	IsActive   *bool                 `json:"isActive" validate:"required"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type UserRoleAccessesCreateRequest struct {
	UserRoleId        int  `json:"userRoleId" validate:"required"`
	MenuId            int  `json:"menuId" validate:"required"`
	IsViewEnabled     bool `json:"isViewEnabled" validate:"required"`
	IsInsertEnabled   bool `json:"isInsertEnabled" validate:"required"`
	IsUpdateEnabled   bool `json:"isUpdateEnabled" validate:"required"`
	IsDeleteEnabled   bool `json:"isDeleteEnabled" validate:"required"`
	IsApprovalEnabled bool `json:"isApprovalEnabled" validate:"required"`
	IsAdminEnabled    bool `json:"isAdminEnabled" validate:"required"`
}

func (req UserRoleAccessesCreateRequest) ToEntity() *entity.UserRoleAccesses {
	return &entity.UserRoleAccesses{
		UserRoleId:        req.UserRoleId,
		MenuId:            req.MenuId,
		IsViewEnabled:     req.IsViewEnabled,
		IsInsertEnabled:   req.IsInsertEnabled,
		IsUpdateEnabled:   req.IsUpdateEnabled,
		IsDeleteEnabled:   req.IsDeleteEnabled,
		IsApprovalEnabled: req.IsApprovalEnabled,
		IsAdminEnabled:    req.IsAdminEnabled,
	}
}

type UserRoleAccessesUpdateRequest struct {
	ID                uint `json:"id" validate:"required"`
	UserRoleId        int  `json:"user_role_id" validate:"required"`
	MenuId            int  `json:"menu_id" validate:"required"`
	IsViewEnabled     bool `json:"is_view_enabled" validate:"required"`
	IsInsertEnabled   bool `json:"is_insert_enabled" validate:"required"`
	IsUpdateEnabled   bool `json:"is_update_enabled" validate:"required"`
	IsDeleteEnabled   bool `json:"is_delete_enabled" validate:"required"`
	IsApprovalEnabled bool `json:"is_approval_enabled" validate:"required"`
	IsAdminEnabled    bool `json:"is_admin_enabled" validate:"required"`
}

func (req UserRoleAccessesUpdateRequest) ToEntity() *entity.UserRoleAccesses {
	return &entity.UserRoleAccesses{
		ID:                req.ID,
		UserRoleId:        req.UserRoleId,
		MenuId:            req.MenuId,
		IsViewEnabled:     req.IsViewEnabled,
		IsInsertEnabled:   req.IsInsertEnabled,
		IsUpdateEnabled:   req.IsUpdateEnabled,
		IsDeleteEnabled:   req.IsDeleteEnabled,
		IsApprovalEnabled: req.IsApprovalEnabled,
		IsAdminEnabled:    req.IsAdminEnabled,
		UpdatedAt:         time.Now(),
	}
}

type UserRoleAccessesQueryRequestContext struct {
	UserRoleId string `json:"userRoleId"`
	MenuId     string `json:"menuId"`
}

func (req UserRoleAccessesQueryRequestContext) ToParamRequest() UserRoleAccessesQueryRequest {
	var request UserRoleAccessesQueryRequest

	if userRoleIdStr := req.UserRoleId; userRoleIdStr != "" {
		userRoleId, err := strconv.Atoi(userRoleIdStr)
		if err == nil {
			request.UserRoleId = &userRoleId
		}
	}

	if menuIdStr := req.MenuId; menuIdStr != "" {
		menuId, err := strconv.Atoi(menuIdStr)
		if err == nil {
			request.MenuId = &menuId
		}
	}

	return request
}
