package request

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

type CampaignFilesQueryRequest struct {
	CampaignID *uint                 `json:"campaignId"`
	Type       *string               `json:"type"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type CampaignFilesCreateRequest struct {
	CampaignID  uint    `json:"campaign_id" validate:"required"`
	Type        string  `json:"type" validate:"required"` // url, file
	FileURL     *string `json:"file_url"`
	ExternalURL *string `json:"external_url"`
	IsDraft     *bool   `json:"is_draft"`
	IsPublish   *bool   `json:"is_publish"`
}

func (req CampaignFilesCreateRequest) ToEntity() *entity.CampaignFiles {
	isDraft := false
	isPublish := false
	if req.IsDraft != nil {
		isDraft = *req.IsDraft
	}
	if req.IsPublish != nil {
		isPublish = *req.IsPublish
	}
	return &entity.CampaignFiles{
		CampaignID:  req.CampaignID,
		Type:        req.Type,
		FileURL:     req.FileURL,
		ExternalURL: req.ExternalURL,
		IsDraft:     &isDraft,
		IsPublish:   &isPublish,
		CreatedAt:   time.Now(),
		UpdatedAt:   time.Now(),
	}
}

type CampaignFilesUpdateRequest struct {
	Type        string  `json:"type" validate:"required"`
	FileURL     *string `json:"file_url"`
	ExternalURL *string `json:"external_url"`
	IsDraft     *bool   `json:"is_draft"`
	IsPublish   *bool   `json:"is_publish"`
}

func (req CampaignFilesUpdateRequest) ToEntity() *entity.CampaignFiles {
	return &entity.CampaignFiles{
		Type:        req.Type,
		FileURL:     req.FileURL,
		ExternalURL: req.ExternalURL,
		IsDraft:     req.IsDraft,
		IsPublish:   req.IsPublish,
		UpdatedAt:   time.Now(),
	}
}

type CampaignFilesQueryRequestContext struct {
	CampaignID string `json:"campaignId"`
	Type       string `json:"type"`
}

func (req CampaignFilesQueryRequestContext) ToParamRequest() CampaignFilesQueryRequest {
	var request CampaignFilesQueryRequest

	if campaignIDStr := req.CampaignID; campaignIDStr != "" {
		campaignID, err := strconv.ParseUint(campaignIDStr, 10, 32)
		if err == nil {
			campaignIDUint := uint(campaignID)
			request.CampaignID = &campaignIDUint
		}
	}
	if typeStr := req.Type; typeStr != "" {
		request.Type = &typeStr
	}

	return request
}

