package request

import (
	"jaecoo-be/app/database/entity"
	"jaecoo-be/utils/paginator"
	"strconv"
)

type ProductSpecificationsQueryRequest struct {
	ProductID  *uint                 `json:"product_id"`
	Title      *string               `json:"title"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type ProductSpecificationsQueryRequestContext struct {
	ProductID string `json:"product_id"`
	Title     string `json:"title"`
}

func (req ProductSpecificationsQueryRequestContext) ToParamRequest() ProductSpecificationsQueryRequest {
	var request ProductSpecificationsQueryRequest

	if productIDStr := req.ProductID; productIDStr != "" {
		productID, err := strconv.Atoi(productIDStr)
		if err == nil {
			productIDUint := uint(productID)
			request.ProductID = &productIDUint
		}
	}
	if title := req.Title; title != "" {
		request.Title = &title
	}

	return request
}

type ProductSpecificationsCreateRequest struct {
	ProductID     uint    `json:"product_id" validate:"required"`
	Title         string  `json:"title" validate:"required"`
	ThumbnailPath *string `json:"thumbnail_path"`
}

func (req ProductSpecificationsCreateRequest) ToEntity() *entity.ProductSpecifications {
	return &entity.ProductSpecifications{
		ProductID:     req.ProductID,
		Title:         req.Title,
		ThumbnailPath: req.ThumbnailPath,
	}
}

type ProductSpecificationsUpdateRequest struct {
	ProductID     *uint   `json:"product_id"`
	Title         *string `json:"title"`
	ThumbnailPath *string `json:"thumbnail_path"`
	IsActive      *bool   `json:"is_active"`
}

func (req ProductSpecificationsUpdateRequest) ToEntity() *entity.ProductSpecifications {
	return &entity.ProductSpecifications{
		ProductID:     getUintValue(req.ProductID),
		Title:         getStringValue(req.Title),
		ThumbnailPath: req.ThumbnailPath,
		IsActive:      req.IsActive,
	}
}

func getStringValue(s *string) string {
	if s == nil {
		return ""
	}
	return *s
}

func getUintValue(u *uint) uint {
	if u == nil {
		return 0
	}
	return *u
}

