package request

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

type CampaignsQueryRequest struct {
	Title          *string               `json:"title"`
	CampaignTypeID *uint                 `json:"campaignTypeId"`
	Status         *string               `json:"status"`
	CreatorID      *uint                 `json:"creatorId"`
	Pagination     *paginator.Pagination `json:"pagination"`
}

type CampaignsCreateRequest struct {
	Title             string     `json:"title" validate:"required"`
	CampaignTypeID    uint       `json:"campaign_type_id" validate:"required"`
	StartDate         *string    `json:"start_date"`
	EndDate           *string    `json:"end_date"`
	MediaTypeSelected *string    `json:"media_type_selected"`
	MediaItemSelected *string    `json:"media_item_selected"`
	Purpose           *string     `json:"purpose"`
	MediaPromote      *bool       `json:"media_promote"`
	Description       *string     `json:"description"`
	CreatorID         *uint       `json:"creator_id"`
	Status            *string     `json:"status"`
}

func (req CampaignsCreateRequest) ToEntity() *entity.Campaigns {
	campaign := &entity.Campaigns{
		Title:              req.Title,
		CampaignTypeID:     req.CampaignTypeID,
		MediaTypeSelected:  req.MediaTypeSelected,
		MediaItemSelected:  req.MediaItemSelected,
		Purpose:            req.Purpose,
		MediaPromote:       req.MediaPromote,
		Description:        req.Description,
		CreatedAt:          time.Now(),
		UpdatedAt:          time.Now(),
	}

	if req.StartDate != nil && *req.StartDate != "" {
		if startDate, err := time.Parse("2006-01-02", *req.StartDate); err == nil {
			campaign.StartDate = &startDate
		}
	}
	if req.EndDate != nil && *req.EndDate != "" {
		if endDate, err := time.Parse("2006-01-02", *req.EndDate); err == nil {
			campaign.EndDate = &endDate
		}
	}
	if req.Status != nil {
		campaign.Status = *req.Status
	} else {
		campaign.Status = "draft"
	}

	return campaign
}

type CampaignsUpdateRequest struct {
	Title             string     `json:"title" validate:"required"`
	CampaignTypeID    uint       `json:"campaign_type_id" validate:"required"`
	StartDate         *string    `json:"start_date"`
	EndDate           *string    `json:"end_date"`
	MediaTypeSelected *string    `json:"media_type_selected"`
	MediaItemSelected *string    `json:"media_item_selected"`
	Purpose           *string     `json:"purpose"`
	MediaPromote      *bool       `json:"media_promote"`
	Description       *string     `json:"description"`
	Status            *string     `json:"status"`
}

func (req CampaignsUpdateRequest) ToEntity() *entity.Campaigns {
	campaign := &entity.Campaigns{
		Title:              req.Title,
		CampaignTypeID:     req.CampaignTypeID,
		MediaTypeSelected:  req.MediaTypeSelected,
		MediaItemSelected:  req.MediaItemSelected,
		Purpose:            req.Purpose,
		MediaPromote:       req.MediaPromote,
		Description:        req.Description,
		UpdatedAt:          time.Now(),
	}

	if req.StartDate != nil && *req.StartDate != "" {
		if startDate, err := time.Parse("2006-01-02", *req.StartDate); err == nil {
			campaign.StartDate = &startDate
		}
	}
	if req.EndDate != nil && *req.EndDate != "" {
		if endDate, err := time.Parse("2006-01-02", *req.EndDate); err == nil {
			campaign.EndDate = &endDate
		}
	}
	if req.Status != nil {
		campaign.Status = *req.Status
	}

	return campaign
}

type CampaignsQueryRequestContext struct {
	Title          string `json:"title"`
	CampaignTypeID string `json:"campaignTypeId"`
	Status         string `json:"status"`
	CreatorID      string `json:"creatorId"`
}

func (req CampaignsQueryRequestContext) ToParamRequest() CampaignsQueryRequest {
	var request CampaignsQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if campaignTypeIDStr := req.CampaignTypeID; campaignTypeIDStr != "" {
		campaignTypeID, err := strconv.ParseUint(campaignTypeIDStr, 10, 32)
		if err == nil {
			campaignTypeIDUint := uint(campaignTypeID)
			request.CampaignTypeID = &campaignTypeIDUint
		}
	}
	if status := req.Status; status != "" {
		request.Status = &status
	}
	if creatorIDStr := req.CreatorID; creatorIDStr != "" {
		creatorID, err := strconv.ParseUint(creatorIDStr, 10, 32)
		if err == nil {
			creatorIDUint := uint(creatorID)
			request.CreatorID = &creatorIDUint
		}
	}

	return request
}

