package request

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/utils/paginator"
	"strconv"
	"time"
)

type FeedbacksGeneric interface {
	ToEntity()
}

type FeedbacksQueryRequest struct {
	Message          *string               `json:"message"`
	CommentFromName  *string               `json:"commentFromName"`
	CommentFromEmail *string               `json:"commentFromEmail"`
	StartDate        *string               `json:"startDate"`
	EndDate          *string               `json:"endDate"`
	StatusId         *int                  `json:"statusId"`
	Pagination       *paginator.Pagination `json:"pagination"`
}

type FeedbacksCreateRequest struct {
	Message          string `json:"message" validate:"required"`
	CommentFromName  string `json:"commentFromName" validate:"required"`
	CommentFromEmail string `json:"commentFromEmail" validate:"required"`
}

func (req FeedbacksCreateRequest) ToEntity() *entity.Feedbacks {
	return &entity.Feedbacks{
		Message:          req.Message,
		CommentFromName:  req.CommentFromName,
		CommentFromEmail: req.CommentFromEmail,
		StatusId:         0,
		IsActive:         true,
	}
}

type FeedbacksUpdateRequest struct {
	ID               uint   `json:"id" validate:"required"`
	Message          string `json:"message" validate:"required"`
	CommentFromName  string `json:"commentFromName" validate:"required"`
	CommentFromEmail string `json:"commentFromEmail" validate:"required"`
}

func (req FeedbacksUpdateRequest) ToEntity() *entity.Feedbacks {
	return &entity.Feedbacks{
		ID:               req.ID,
		Message:          req.Message,
		CommentFromName:  req.CommentFromName,
		CommentFromEmail: req.CommentFromEmail,
		UpdatedAt:        time.Now(),
	}
}

type FeedbacksQueryRequestContext struct {
	Message          string `json:"message"`
	CommentFromName  string `json:"commentFromName"`
	CommentFromEmail string `json:"commentFromEmail"`
	StatusId         string `json:"statusId"`
}

func (req FeedbacksQueryRequestContext) ToParamRequest() FeedbacksQueryRequest {
	var request FeedbacksQueryRequest

	if message := req.Message; message != "" {
		request.Message = &message
	}
	if commentFromName := req.CommentFromName; commentFromName != "" {
		request.CommentFromName = &commentFromName
	}
	if commentFromEmail := req.CommentFromEmail; commentFromEmail != "" {
		request.CommentFromEmail = &commentFromEmail
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}

	return request
}
