package request

import (
	"narasi-ahli-be/app/database/entity/user_levels"
	"narasi-ahli-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"`
	ProvinceId       *int    `json:"provinceId"`
	Group            *string `json:"group"`
	IsApprovalActive *bool   `json:"isApprovalActive"`
	IsActive         *bool   `json:"isActive"`
}

func (req UserLevelsCreateRequest) ToEntity() *user_levels.UserLevels {
	return &user_levels.UserLevels{
		Name:             req.Name,
		AliasName:        req.AliasName,
		LevelNumber:      req.LevelNumber,
		ParentLevelId:    req.ParentLevelId,
		ProvinceId:       req.ProvinceId,
		IsApprovalActive: req.IsApprovalActive,
		Group:            req.Group,
		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"`
	IsApprovalActive *bool   `json:"isApprovalActive"`
	Group            *string `json:"group"`
	ProvinceId       *int    `json:"provinceId"`
}

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

type UserLevelsApprovalRequest struct {
	Ids              string `json:"ids" validate:"required"`
	IsApprovalActive bool   `json:"isApprovalActive" validate:"required"`
}

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
}
