package request

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/utils/paginator"
	"strconv"
	"time"
)

type CampaignDestinationsQueryRequest struct {
	Name            *string               `json:"name"`
	CampaignTypeID  *uint                 `json:"campaignTypeId"`
	IsActive        *bool                 `json:"isActive"`
	Pagination      *paginator.Pagination `json:"pagination"`
}

type CampaignDestinationsCreateRequest struct {
	CampaignTypeID uint    `json:"campaign_type_id" validate:"required"`
	SubType        string  `json:"sub_type" validate:"required"`
	Name           string  `json:"name" validate:"required"`
	Description    *string `json:"description"`
	URL            *string `json:"url"`
	IsActive       *bool   `json:"isActive"`
	CreatedById    *uint   `json:"createdById"`
}

func (req CampaignDestinationsCreateRequest) ToEntity() *entity.CampaignDestinations {
	isActive := true
	if req.IsActive != nil {
		isActive = *req.IsActive
	}
	return &entity.CampaignDestinations{
		CampaignTypeID: req.CampaignTypeID,
		SubType:        req.SubType,
		Name:           req.Name,
		Description:    req.Description,
		URL:            req.URL,
		IsActive:       &isActive,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
	}
}

type CampaignDestinationsUpdateRequest struct {
	CampaignTypeID uint    `json:"campaign_type_id" validate:"required"`
	SubType        string  `json:"sub_type" validate:"required"`
	Name           string  `json:"name" validate:"required"`
	Description    *string `json:"description"`
	URL            *string `json:"url"`
	IsActive       *bool   `json:"isActive"`
}

func (req CampaignDestinationsUpdateRequest) ToEntity() *entity.CampaignDestinations {
	return &entity.CampaignDestinations{
		CampaignTypeID: req.CampaignTypeID,
		SubType:        req.SubType,
		Name:           req.Name,
		Description:    req.Description,
		URL:            req.URL,
		IsActive:       req.IsActive,
		UpdatedAt:      time.Now(),
	}
}

type CampaignDestinationsQueryRequestContext struct {
	Name           string `json:"name"`
	CampaignTypeID string `json:"campaignTypeId"`
	IsActive       string `json:"isActive"`
}

func (req CampaignDestinationsQueryRequestContext) ToParamRequest() CampaignDestinationsQueryRequest {
	var request CampaignDestinationsQueryRequest

	if name := req.Name; name != "" {
		request.Name = &name
	}
	if campaignTypeIDStr := req.CampaignTypeID; campaignTypeIDStr != "" {
		campaignTypeID, err := strconv.ParseUint(campaignTypeIDStr, 10, 32)
		if err == nil {
			campaignTypeIDUint := uint(campaignTypeID)
			request.CampaignTypeID = &campaignTypeIDUint
		}
	}
	if isActiveStr := req.IsActive; isActiveStr != "" {
		isActive, err := strconv.ParseBool(isActiveStr)
		if err == nil {
			request.IsActive = &isActive
		}
	}

	return request
}

