package request

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

type MasterModulesGeneric interface {
	ToEntity()
}

type MasterModulesQueryRequest struct {
	Name        *string               `json:"name"`
	Description *string               `json:"description"`
	StatusId    *int                  `json:"statusId"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type MasterModulesCreateRequest struct {
	Name        string `json:"name" validate:"required"`
	Description string `json:"description" validate:"required"`
	PathUrl     string `json:"pathUrl" validate:"required"`
	StatusId    int    `json:"statusId" validate:"required"`
}

func (req MasterModulesCreateRequest) ToEntity() *entity.MasterModules {
	return &entity.MasterModules{
		Name:        req.Name,
		Description: req.Description,
		PathUrl:     req.PathUrl,
		StatusId:    req.StatusId,
	}
}

type MasterModulesUpdateRequest struct {
	ID          uint   `json:"id" validate:"required"`
	Name        string `json:"name" validate:"required"`
	Description string `json:"description" validate:"required"`
	PathUrl     string `json:"pathUrl" validate:"required"`
	StatusId    int    `json:"statusId" validate:"required"`
}

func (req MasterModulesUpdateRequest) ToEntity() *entity.MasterModules {
	return &entity.MasterModules{
		ID:          req.ID,
		Name:        req.Name,
		Description: req.Description,
		PathUrl:     req.PathUrl,
		StatusId:    req.StatusId,
		UpdatedAt:   time.Now(),
	}
}

type MasterModulesQueryRequestContext struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	StatusId    string `json:"statusId"`
}

func (req MasterModulesQueryRequestContext) ToParamRequest() MasterModulesQueryRequest {
	var request MasterModulesQueryRequest

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

	return request
}
