package request

import (
	"strconv"
	"time"
	"web-qudo-be/app/database/entity"
	"web-qudo-be/utils/paginator"
)

type ClientsGeneric interface {
	ToEntity()
}

type ClientsQueryRequest struct {
	Name        *string               `json:"name"`
	CreatedById *uint                 `json:"createdBy"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type ClientsCreateRequest struct {
	Name        string `json:"name" validate:"required"`
	CreatedById *uint  `json:"createdById"`
}

func (req ClientsCreateRequest) ToEntity() *entity.Clients {
	return &entity.Clients{
		Name:        req.Name,
		CreatedById: req.CreatedById,
		CreatedAt:   time.Now(),
	}
}

type ClientsUpdateRequest struct {
	Name        string `json:"name" validate:"required"`
	CreatedById *uint  `json:"createdById"`
}

func (req ClientsUpdateRequest) ToEntity() *entity.Clients {
	return &entity.Clients{
		Name:        req.Name,
		CreatedById: req.CreatedById,
		UpdatedAt:   time.Now(),
	}
}

type ClientsQueryRequestContext struct {
	Name        string `json:"name"`
	CreatedById string `json:"createdById"`
}

func (req ClientsQueryRequestContext) ToParamRequest() ClientsQueryRequest {
	var request ClientsQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if createdByStr := req.CreatedById; createdByStr != "" {
		createdBy, err := strconv.Atoi(createdByStr)
		if err == nil {
			createdByIdUint := uint(createdBy)
			request.CreatedById = &createdByIdUint
		}
	}

	return request
}
