package request

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

type BookmarksGeneric interface {
	ToEntity()
}

type BookmarksQueryRequest struct {
	UserId     *uint                 `json:"userId"`
	ArticleId  *uint                 `json:"articleId"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type BookmarksCreateRequest struct {
	ArticleId uint `json:"articleId" validate:"required"`
}

func (req BookmarksCreateRequest) ToEntity(userId uint) *entity.Bookmarks {
	return &entity.Bookmarks{
		UserId:    userId,
		ArticleId: req.ArticleId,
		IsActive:  boolPtr(true),
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
	}
}

type BookmarksQueryRequestContext struct {
	UserId    string `json:"userId"`
	ArticleId string `json:"articleId"`
}

func (req BookmarksQueryRequestContext) ToParamRequest() BookmarksQueryRequest {
	var request BookmarksQueryRequest

	if userIdStr := req.UserId; userIdStr != "" {
		userId, err := strconv.Atoi(userIdStr)
		if err == nil {
			userIdUint := uint(userId)
			request.UserId = &userIdUint
		}
	}
	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			articleIdUint := uint(articleId)
			request.ArticleId = &articleIdUint
		}
	}

	return request
}

// Helper function to create bool pointer
func boolPtr(b bool) *bool {
	return &b
}
