package request

import (
	"strconv"
	"web-qudo-be/app/database/entity"
	"web-qudo-be/utils/paginator"
)

type ArticleApprovalsGeneric interface {
	ToEntity()
}

type ArticleApprovalsQueryRequest struct {
	ArticleId       *int                  `json:"articleId"`
	ApprovalBy      *int                  `json:"approvalBy"`
	StatusId        *int                  `json:"statusId"`
	Message         *string               `json:"message"`
	ApprovalAtLevel *int                  `json:"approvalAtLevel"`
	Pagination      *paginator.Pagination `json:"pagination"`
}

type ArticleApprovalsCreateRequest struct {
	ArticleId uint   `json:"articleId" validate:"required"`
	StatusId  int    `json:"statusId" validate:"required"`
	Message   string `json:"message" validate:"required"`
}

func (req ArticleApprovalsCreateRequest) ToEntity() *entity.ArticleApprovals {
	return &entity.ArticleApprovals{
		ArticleId: req.ArticleId,
		StatusId:  req.StatusId,
		Message:   req.Message,
	}
}

type ArticleApprovalsUpdateRequest struct {
	ID        uint   `json:"id" validate:"required"`
	ArticleId uint   `json:"articleId" validate:"required"`
	StatusId  int    `json:"statusId" validate:"required"`
	Message   string `json:"message" validate:"required"`
}

func (req ArticleApprovalsUpdateRequest) ToEntity() *entity.ArticleApprovals {
	return &entity.ArticleApprovals{
		ID:        req.ID,
		ArticleId: req.ArticleId,
		StatusId:  req.StatusId,
		Message:   req.Message,
	}
}

type ArticleApprovalsQueryRequestContext struct {
	ArticleId       string `json:"articleId"`
	ApprovalBy      string `json:"approvalBy"`
	StatusId        string `json:"statusId"`
	Message         string `json:"message"`
	ApprovalAtLevel string `json:"approvalAtLevel"`
}

func (req ArticleApprovalsQueryRequestContext) ToParamRequest() ArticleApprovalsQueryRequest {
	var request ArticleApprovalsQueryRequest

	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			request.ArticleId = &articleId
		}
	}
	if approvalByStr := req.ApprovalBy; approvalByStr != "" {
		approvalBy, err := strconv.Atoi(approvalByStr)
		if err == nil {
			request.ApprovalBy = &approvalBy
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}
	if message := req.Message; message != "" {
		request.Message = &message
	}
	if approvalAtLevelStr := req.ApprovalAtLevel; approvalAtLevelStr != "" {
		approvalAtLevel, err := strconv.Atoi(approvalAtLevelStr)
		if err == nil {
			request.ApprovalAtLevel = &approvalAtLevel
		}
	}

	return request
}
