package request

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

type UserLevelsGeneric interface {
	ToEntity()
}

type UserLevelsQueryRequest struct {
	Name          *string               `json:"name"`
	LevelNumber   *int                  `json:"levelNumber"`
	ParentLevelId *int                  `json:"parentLevelId"`
	ProvinceId    *int                  `json:"provinceId"`
	Pagination    *paginator.Pagination `json:"pagination"`
}

type UserLevelsCreateRequest struct {
	Name          string `json:"name" validate:"required"`
	AliasName     string `json:"aliasName" validate:"required"`
	LevelNumber   int    `json:"levelNumber" validate:"required"`
	ParentLevelId int    `json:"parentLevelId" validate:"required"`
	ProvinceId    int    `json:"provinceId" validate:"required"`
	IsActive      *bool  `json:"isActive"`
}

func (req UserLevelsCreateRequest) ToEntity() *entity.UserLevels {
	return &entity.UserLevels{
		Name:          req.Name,
		AliasName:     req.AliasName,
		LevelNumber:   req.LevelNumber,
		ParentLevelId: req.ParentLevelId,
		ProvinceId:    req.ProvinceId,
		IsActive:      req.IsActive,
	}
}

type UserLevelsUpdateRequest struct {
	Name          string `json:"name" validate:"required"`
	AliasName     string `json:"aliasName" validate:"required"`
	LevelNumber   int    `json:"levelNumber" validate:"required"`
	ParentLevelId int    `json:"parentLevelId" validate:"required"`
	ProvinceId    int    `json:"provinceId" validate:"required"`
}

func (req UserLevelsUpdateRequest) ToEntity() *entity.UserLevels {
	return &entity.UserLevels{
		Name:          req.Name,
		AliasName:     req.AliasName,
		LevelNumber:   req.LevelNumber,
		ParentLevelId: req.ParentLevelId,
		ProvinceId:    req.ProvinceId,
		UpdatedAt:     time.Now(),
	}
}

type UserLevelsQueryRequestContext struct {
	Name          string `json:"name"`
	LevelNumber   string `json:"levelNumber"`
	ParentLevelId string `json:"parentLevelId"`
	ProvinceId    string `json:"provinceId"`
}

func (req UserLevelsQueryRequestContext) ToParamRequest() UserLevelsQueryRequest {
	var request UserLevelsQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if levelNumberStr := req.LevelNumber; levelNumberStr != "" {
		LevelNumber, err := strconv.Atoi(levelNumberStr)
		if err == nil {
			request.LevelNumber = &LevelNumber
		}
	}
	if parentLevelIdStr := req.ParentLevelId; parentLevelIdStr != "" {
		parentLevelId, err := strconv.Atoi(parentLevelIdStr)
		if err == nil {
			request.ParentLevelId = &parentLevelId
		}
	}
	if provinceIdStr := req.ProvinceId; provinceIdStr != "" {
		provinceId, err := strconv.Atoi(provinceIdStr)
		if err == nil {
			request.ProvinceId = &provinceId
		}
	}

	return request
}
