package request

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

type UserRolesGeneric interface {
	ToEntity()
}

type UserRolesQueryRequest struct {
	Name        *string               `json:"name"`
	Description *string               `json:"description"`
	Code        *string               `json:"code"`
	LevelNumber *int                  `json:"levelNumber"`
	StatusId    *int                  `json:"statusId"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type UserRolesCreateRequest struct {
	Name         string `json:"name" validate:"required"`
	Description  string `json:"description" validate:"required"`
	Code         string `json:"code" validate:"required"`
	LevelNumber  int    `json:"levelNumber" validate:"required"`
	StatusId     int    `json:"statusId" validate:"required"`
	UserLevelIds []int  `json:"userLevelIds" validate:"required"`
}

func (req UserRolesCreateRequest) ToEntity() *entity.UserRoles {
	return &entity.UserRoles{
		Name:        req.Name,
		Description: req.Description,
		Code:        req.Code,
		LevelNumber: req.LevelNumber,
		StatusId:    req.StatusId,
	}
}

type UserRolesUpdateRequest struct {
	Name         string `json:"name" validate:"required"`
	Description  string `json:"description" validate:"required"`
	Code         string `json:"code" validate:"required"`
	LevelNumber  int    `json:"level_number" validate:"required"`
	StatusId     int    `json:"status_id" validate:"required"`
	UserLevelIds []int  `json:"userLevelIds" validate:"required"`
}

func (req UserRolesUpdateRequest) ToEntity() *entity.UserRoles {
	return &entity.UserRoles{
		Name:        req.Name,
		Description: req.Description,
		Code:        req.Code,
		LevelNumber: req.LevelNumber,
		StatusId:    req.StatusId,
		UpdatedAt:   time.Now(),
	}
}

type UserRolesQueryRequestContext struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Code        string `json:"code"`
	LevelNumber string `json:"levelNumber"`
	StatusId    string `json:"statusId"`
}

func (req UserRolesQueryRequestContext) ToParamRequest() UserRolesQueryRequest {
	var request UserRolesQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if code := req.Code; code != "" {
		request.Code = &code
	}
	if levelNumberStr := req.LevelNumber; levelNumberStr != "" {
		levelNumber, err := strconv.Atoi(levelNumberStr)
		if err == nil {
			request.LevelNumber = &levelNumber
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}

	return request
}
