package request

import (
	"narasi-ahli-be/app/database/entity"
	"narasi-ahli-be/utils/paginator"
	"strconv"
	"time"
)

type ActivityLogsGeneric interface {
	ToEntity()
}

type ActivityLogsQueryRequest struct {
	ActivityTypeId *int                  `json:"activityTypeId"`
	Url            *string               `json:"url"`
	ArticleId      *int                  `json:"articleId"`
	UserId         *int                  `json:"userId"`
	Pagination     *paginator.Pagination `json:"pagination"`
}

type ActivityLogsCreateRequest struct {
	ActivityTypeId int     `json:"activityTypeId" validate:"required"`
	Url            string  `json:"url" validate:"required"`
	ArticleId      *uint   `json:"articleId"`
	UserId         *uint   `json:"userId"`
	VisitorIp      *string `json:"visitorIp"`
}

func (req ActivityLogsCreateRequest) ToEntity() *entity.ActivityLogs {
	return &entity.ActivityLogs{
		ActivityTypeId: req.ActivityTypeId,
		Url:            req.Url,
		ArticleId:      req.ArticleId,
		UserId:         req.UserId,
		VisitorIp:      req.VisitorIp,
		CreatedAt:      time.Now(),
	}
}

type ActivityLogsUpdateRequest struct {
	ID             uint   `json:"id" validate:"required"`
	ActivityTypeId int    `json:"activityTypeId" validate:"required"`
	Url            string `json:"url" validate:"required"`
	ArticleId      *uint  `json:"articleId"`
	UserId         *uint  `json:"userId"`
}

func (req ActivityLogsUpdateRequest) ToEntity() *entity.ActivityLogs {
	return &entity.ActivityLogs{
		ID:             req.ID,
		ActivityTypeId: req.ActivityTypeId,
		Url:            req.Url,
		ArticleId:      req.ArticleId,
		UserId:         req.UserId,
	}
}

type ActivityLogsQueryRequestContext struct {
	ActivityTypeId string `json:"activityTypeId"`
	Url            string `json:"url"`
	ArticleId      string `json:"articleId"`
	UserId         string `json:"userId"`
}

func (req ActivityLogsQueryRequestContext) ToParamRequest() ActivityLogsQueryRequest {
	var request ActivityLogsQueryRequest

	if activityTypeIdStr := req.ActivityTypeId; activityTypeIdStr != "" {
		activityTypeId, err := strconv.Atoi(activityTypeIdStr)
		if err == nil {
			request.ActivityTypeId = &activityTypeId
		}
	}
	if url := req.Url; url != "" {
		request.Url = &url
	}
	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			request.ArticleId = &articleId
		}
	}
	if userIdStr := req.UserId; userIdStr != "" {
		userId, err := strconv.Atoi(userIdStr)
		if err == nil {
			request.UserId = &userId
		}
	}

	return request
}
