package request

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

type ArticlesGeneric interface {
	ToEntity()
}

type ArticlesQueryRequest struct {
	Title       *string               `json:"title"`
	Description *string               `json:"description"`
	CategoryId  *uint                 `json:"categoryId"`
	Category    *string               `json:"category"`
	TypeId      *int                  `json:"typeId"`
	Tags        *string               `json:"tags"`
	CreatedById *int                  `json:"createdById"`
	StatusId    *int                  `json:"statusId"`
	IsBanner    *bool                 `json:"isBanner"`
	IsPublish   *bool                 `json:"isPublish"`
	IsDraft     *bool                 `json:"isDraft"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type ArticlesCreateRequest struct {
	Title           string  `json:"title" validate:"required"`
	Slug            string  `json:"slug" validate:"required"`
	Description     string  `json:"description" validate:"required"`
	HtmlDescription string  `json:"htmlDescription" validate:"required"`
	CategoryIds     string  `json:"categoryIds" validate:"required"`
	TypeId          int     `json:"typeId" validate:"required"`
	Tags            string  `json:"tags" validate:"required"`
	AiArticleId     *int    `json:"aiArticleId"`
	CreatedAt       *string `json:"createdAt"`
	CreatedById     *uint   `json:"createdById"`
	IsPublish       *bool   `json:"isPublish"`
	IsDraft         *bool   `json:"isDraft"`
	OldId           *uint   `json:"oldId"`
}

func (req ArticlesCreateRequest) ToEntity() *entity.Articles {
	return &entity.Articles{
		Title:           req.Title,
		Slug:            req.Slug,
		Description:     req.Description,
		HtmlDescription: req.HtmlDescription,
		TypeId:          req.TypeId,
		Tags:            req.Tags,
		AiArticleId:     req.AiArticleId,
		IsPublish:       req.IsPublish,
		IsDraft:         req.IsDraft,
		OldId:           req.OldId,
	}
}

type ArticlesUpdateRequest struct {
	Title           string  `json:"title" validate:"required"`
	Slug            string  `json:"slug" validate:"required"`
	Description     string  `json:"description" validate:"required"`
	HtmlDescription string  `json:"htmlDescription" validate:"required"`
	CategoryIds     string  `json:"categoryIds" validate:"required"`
	TypeId          int     `json:"typeId" validate:"required"`
	Tags            string  `json:"tags" validate:"required"`
	CreatedAt       *string `json:"createdAt"`
	CreatedById     *uint   `json:"createdById"`
	AiArticleId     *int    `json:"aiArticleId"`
	IsPublish       *bool   `json:"isPublish"`
	IsDraft         *bool   `json:"isDraft"`
	StatusId        *int    `json:"statusId"`
}

func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
	if req.CreatedById == nil {
		return &entity.Articles{
			Title:           req.Title,
			Slug:            req.Slug,
			Description:     req.Description,
			HtmlDescription: req.HtmlDescription,
			TypeId:          req.TypeId,
			Tags:            req.Tags,
			StatusId:        req.StatusId,
			AiArticleId:     req.AiArticleId,
			IsPublish:       req.IsPublish,
			IsDraft:         req.IsDraft,
			UpdatedAt:       time.Now(),
		}
	} else {
		return &entity.Articles{
			Title:           req.Title,
			Slug:            req.Slug,
			Description:     req.Description,
			HtmlDescription: req.HtmlDescription,
			TypeId:          req.TypeId,
			Tags:            req.Tags,
			StatusId:        req.StatusId,
			CreatedById:     req.CreatedById,
			AiArticleId:     req.AiArticleId,
			IsPublish:       req.IsPublish,
			IsDraft:         req.IsDraft,
			UpdatedAt:       time.Now(),
		}
	}
}

type ArticlesQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	CategoryId  string `json:"categoryId"`
	Category    string `json:"category"`
	TypeId      string `json:"typeId"`
	Tags        string `json:"tags"`
	CreatedById string `json:"createdById"`
	IsBanner    string `json:"isBanner"`
	IsPublish   string `json:"isPublish"`
	IsDraft     string `json:"isDraft"`
	StatusId    string `json:"statusId"`
}

func (req ArticlesQueryRequestContext) ToParamRequest() ArticlesQueryRequest {
	var request ArticlesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if category := req.Category; category != "" {
		request.Category = &category
	}
	if categoryIdStr := req.CategoryId; categoryIdStr != "" {
		categoryId, err := strconv.Atoi(categoryIdStr)
		if err == nil {
			categoryIdUint := uint(categoryId)
			request.CategoryId = &categoryIdUint
		}
	}
	if typeIdStr := req.TypeId; typeIdStr != "" {
		typeId, err := strconv.Atoi(typeIdStr)
		if err == nil {
			request.TypeId = &typeId
		}
	}
	if tags := req.Tags; tags != "" {
		request.Tags = &tags
	}
	if isPublishStr := req.IsPublish; isPublishStr != "" {
		isPublish, err := strconv.ParseBool(isPublishStr)
		if err == nil {
			request.IsPublish = &isPublish
		}
	}
	if isBannerStr := req.IsBanner; isBannerStr != "" {
		isBanner, err := strconv.ParseBool(isBannerStr)
		if err == nil {
			request.IsBanner = &isBanner
		}
	}
	if isDraftStr := req.IsDraft; isDraftStr != "" {
		isDraft, err := strconv.ParseBool(isDraftStr)
		if err == nil {
			request.IsDraft = &isDraft
		}
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}
	if createdByIdStr := req.CreatedById; createdByIdStr != "" {
		createdById, err := strconv.Atoi(createdByIdStr)
		if err == nil {
			request.CreatedById = &createdById
		}
	}

	return request
}
