package request

import (
	"go-humas-be/app/database/entity"
	"go-humas-be/utils/paginator"
	"strconv"
	"time"
)

type PpidDataCategoriesGeneric interface {
	ToEntity()
}

type PpidDataCategoriesQueryRequest struct {
	Title       *string               `json:"title"`
	Description *string               `json:"description"`
	IsOnlyTop   *bool                 `json:"isOnlyTop"`
	ParentId    *uint                 `json:"parentId"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type PpidDataCategoriesCreateRequest struct {
	Title       string `json:"title" validate:"required"`
	Description string `json:"description" validate:"required"`
	Slug        string `json:"slug" validate:"required"`
	ParentId    *uint  `json:"parentId"`
}

func (req PpidDataCategoriesCreateRequest) ToEntity() *entity.PpidDataCategories {
	return &entity.PpidDataCategories{
		Title:       req.Title,
		Description: req.Description,
		Slug:        req.Slug,
		ParentId:    req.ParentId,
	}
}

type PpidDataCategoriesUpdateRequest struct {
	ID          uint   `json:"id" validate:"required"`
	Title       string `json:"title" validate:"required"`
	Description string `json:"description" validate:"required"`
	Slug        string `json:"slug" validate:"required"`
	ParentId    *uint  `json:"parentId"`
}

func (req PpidDataCategoriesUpdateRequest) ToEntity() *entity.PpidDataCategories {
	return &entity.PpidDataCategories{
		ID:          req.ID,
		Title:       req.Title,
		Description: req.Description,
		Slug:        req.Slug,
		ParentId:    req.ParentId,
		UpdatedAt:   time.Now(),
	}
}

type PpidDataCategoriesQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	IsOnlyTop   string `json:"isOnlyTop"`
	ParentId    string `json:"parentId"`
}

func (req PpidDataCategoriesQueryRequestContext) ToParamRequest() PpidDataCategoriesQueryRequest {
	var request PpidDataCategoriesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if parentIdStr := req.ParentId; parentIdStr != "" {
		parentId, err := strconv.ParseUint(parentIdStr, 10, 0)
		if err == nil {
			*request.ParentId = uint(parentId)
		}
	}
	if isOnlyTopStr := req.IsOnlyTop; isOnlyTopStr != "" {
		isOnlyTop, err := strconv.ParseBool(isOnlyTopStr)
		if err == nil {
			request.IsOnlyTop = &isOnlyTop
		}
	}

	return request
}
