package request

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

type AdvertisementGeneric interface {
	ToEntity()
}

type AdvertisementQueryRequest struct {
	Title        *string               `json:"title"`
	Description  *string               `json:"description"`
	RedirectLink *string               `json:"redirectLink"`
	Placement    *string               `json:"placement"`
	IsPublish    *bool                 `json:"isPublish"`
	StatusId     *int                  `json:"statusId"`
	Pagination   *paginator.Pagination `json:"pagination"`
}

type AdvertisementCreateRequest struct {
	Title        string `json:"title" validate:"required"`
	Description  string `json:"description" validate:"required"`
	RedirectLink string `json:"redirectLink" validate:"required"`
	Placement    string `json:"placement" validate:"required"`
}

func (req AdvertisementCreateRequest) ToEntity() *entity.Advertisement {
	return &entity.Advertisement{
		Title:        req.Title,
		Description:  req.Description,
		RedirectLink: req.RedirectLink,
		Placement:    req.Placement,
		StatusId:     1,
		IsPublish:    true,
		IsActive:     true,
	}
}

type AdvertisementUpdateRequest struct {
	ID           uint   `json:"id" validate:"required"`
	Title        string `json:"title" validate:"required"`
	Description  string `json:"description" validate:"required"`
	RedirectLink string `json:"redirectLink" validate:"required"`
	Placement    string `json:"placement" validate:"required"`
}

func (req AdvertisementUpdateRequest) ToEntity() *entity.Advertisement {
	return &entity.Advertisement{
		ID:           req.ID,
		Title:        req.Title,
		Description:  req.Description,
		RedirectLink: req.RedirectLink,
		Placement:    req.Placement,
		UpdatedAt:    time.Now(),
	}
}

type AdvertisementQueryRequestContext struct {
	Title        string `json:"title"`
	Description  string `json:"description"`
	RedirectLink string `json:"redirectLink"`
	Placement    string `json:"placement"`
	StatusId     string `json:"statusId"`
	IsPublish    string `json:"isPublish"`
}

func (req AdvertisementQueryRequestContext) ToParamRequest() AdvertisementQueryRequest {
	var request AdvertisementQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if redirectLink := req.RedirectLink; redirectLink != "" {
		request.RedirectLink = &redirectLink
	}
	if placement := req.Placement; placement != "" {
		request.Placement = &placement
	}
	if isPublishStr := req.IsPublish; isPublishStr != "" {
		isPublish, err := strconv.ParseBool(isPublishStr)
		if err == nil {
			request.IsPublish = &isPublish
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}

	return request
}
