package request

import (
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/utils/paginator"
	"strconv"
	"time"
)

type ArticleCommentsGeneric interface {
	ToEntity()
}

type ArticleCommentsQueryRequest struct {
	Message     *string               `json:"message"`
	ArticleId   *int                  `json:"articleId"`
	CommentFrom *int                  `json:"commentFrom"`
	ParentId    *int                  `json:"parentId"`
	IsPublic    *bool                 `json:"isPublic"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type ArticleCommentsCreateRequest struct {
	Message   string `json:"message" validate:"required"`
	ArticleId uint   `json:"articleId" validate:"required"`
	ParentId  *int   `json:"parentId"`
	IsPublic  *bool  `json:"isPublic"`
}

func (req ArticleCommentsCreateRequest) ToEntity() *entity.ArticleComments {
	return &entity.ArticleComments{
		Message:   req.Message,
		ArticleId: req.ArticleId,
		ParentId:  req.ParentId,
		StatusId:  0,
	}
}

type ArticleCommentsUpdateRequest struct {
	ID        uint   `json:"id" validate:"required"`
	Message   string `json:"message" validate:"required"`
	ArticleId uint   `json:"articleId" validate:"required"`
	ParentId  *int   `json:"parentId"`
	IsPublic  *bool  `json:"isPublic"`
}

func (req ArticleCommentsUpdateRequest) ToEntity() *entity.ArticleComments {
	return &entity.ArticleComments{
		ID:        req.ID,
		Message:   req.Message,
		ArticleId: req.ArticleId,
		ParentId:  req.ParentId,
		UpdatedAt: time.Now(),
	}
}

type ArticleCommentsApprovalRequest struct {
	ID       uint `json:"id" validate:"required"`
	StatusId int  `json:"statusId" validate:"required"`
}

func (req ArticleCommentsApprovalRequest) ToEntity() *entity.ArticleComments {
	approvedNow := time.Now()
	return &entity.ArticleComments{
		ID:         req.ID,
		StatusId:   req.StatusId,
		ApprovedAt: &approvedNow,
	}
}

type ArticleCommentsQueryRequestContext struct {
	Message     string `json:"message"`
	ArticleId   string `json:"articleId"`
	CommentFrom string `json:"commentFrom"`
	ParentId    string `json:"parentId"`
	IsPublic    string `json:"isPublic"`
}

func (req ArticleCommentsQueryRequestContext) ToParamRequest() ArticleCommentsQueryRequest {
	var request ArticleCommentsQueryRequest

	if message := req.Message; message != "" {
		request.Message = &message
	}
	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			request.ArticleId = &articleId
		}
	}
	if commentFromStr := req.CommentFrom; commentFromStr != "" {
		commentFrom, err := strconv.Atoi(commentFromStr)
		if err == nil {
			request.CommentFrom = &commentFrom
		}
	}
	if parentIdStr := req.ParentId; parentIdStr != "" {
		parentId, err := strconv.Atoi(parentIdStr)
		if err == nil {
			request.ParentId = &parentId
		}
	}
	if isPublicStr := req.IsPublic; isPublicStr != "" {
		isPublic, err := strconv.ParseBool(isPublicStr)
		if err == nil {
			request.IsPublic = &isPublic
		}
	}

	return request
}
