package request

import (
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/utils/paginator"
	"strconv"
	"time"
)

type ArticleNulisAIGeneric interface {
	ToEntity()
}

type ArticleNulisAIQueryRequest struct {
	NulisAiId       *int                  `json:"nulisAiId"`
	ArticleId       *int                  `json:"articleId"`
	Title           *string               `json:"title"`
	Description     *string               `json:"description"`
	HtmlDescription *string               `json:"htmlDescription"`
	CategoryId      *int                  `json:"categoryId"`
	CreatorId       *int                  `json:"creatorId"`
	Tags            *string               `json:"tags"`
	Pagination      *paginator.Pagination `json:"pagination"`
}

type ArticleNulisAICreateRequest struct {
	NulisAiId       int    `json:"nulisAiId" validate:"required"`
	ArticleId       int    `json:"articleId" validate:"required"`
	Title           string `json:"title" validate:"required"`
	Description     string `json:"description" validate:"required"`
	HtmlDescription string `json:"htmlDescription" validate:"required"`
	CategoryId      int    `json:"categoryId" validate:"required"`
	CreatorId       uint   `json:"creatorId" validate:"required"`
	Tags            string `json:"tags" validate:"required"`
}

func (req ArticleNulisAICreateRequest) ToEntity() *entity.ArticleNulisAI {
	return &entity.ArticleNulisAI{
		NulisAiId:       req.NulisAiId,
		ArticleId:       req.ArticleId,
		Title:           req.Title,
		Description:     req.Description,
		HtmlDescription: req.HtmlDescription,
		CategoryId:      req.CategoryId,
		CreatorId:       req.CreatorId,
		Tags:            req.Tags,
	}
}

type ArticleNulisAIUpdateRequest struct {
	ID              uint      `json:"id" validate:"required"`
	NulisAiId       int       `json:"nulisAiId" validate:"required"`
	ArticleId       int       `json:"articleId" validate:"required"`
	Title           string    `json:"title" validate:"required"`
	Description     string    `json:"description" validate:"required"`
	HtmlDescription string    `json:"htmlDescription" validate:"required"`
	CategoryId      int       `json:"categoryId" validate:"required"`
	CreatorId       uint      `json:"creatorId" validate:"required"`
	Tags            string    `json:"tags" validate:"required"`
	UpdatedAt       time.Time `json:"updated_at"`
}

func (req ArticleNulisAIUpdateRequest) ToEntity() *entity.ArticleNulisAI {
	return &entity.ArticleNulisAI{
		ID:              req.ID,
		NulisAiId:       req.NulisAiId,
		ArticleId:       req.ArticleId,
		Title:           req.Title,
		Description:     req.Description,
		HtmlDescription: req.HtmlDescription,
		CategoryId:      req.CategoryId,
		CreatorId:       req.CreatorId,
		Tags:            req.Tags,
		UpdatedAt:       time.Now(),
	}
}

type ArticleNulisAIQueryRequestContext struct {
	NulisAiId       string `json:"nulisAiId"`
	ArticleId       string `json:"articleId"`
	Title           string `json:"title"`
	Description     string `json:"description"`
	HtmlDescription string `json:"htmlDescription"`
	CategoryId      string `json:"categoryId"`
	CreatorId       string `json:"creatorId"`
	Tags            string `json:"tags"`
	ThumbnailPath   string `json:"thumbnailPath"`
	IsPublish       string `json:"isPublish"`
	PublishedAt     string `json:"publishedAt"`
}

func (req ArticleNulisAIQueryRequestContext) ToParamRequest() ArticleNulisAIQueryRequest {
	var request ArticleNulisAIQueryRequest

	if nulisAiIdStr := req.NulisAiId; nulisAiIdStr != "" {
		nulisAiId, err := strconv.Atoi(nulisAiIdStr)
		if err == nil {
			request.NulisAiId = &nulisAiId
		}
	}
	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			request.ArticleId = &articleId
		}
	}
	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if htmlDescription := req.HtmlDescription; htmlDescription != "" {
		request.HtmlDescription = &htmlDescription
	}
	if categoryIdStr := req.CategoryId; categoryIdStr != "" {
		categoryId, err := strconv.Atoi(categoryIdStr)
		if err == nil {
			request.CategoryId = &categoryId
		}
	}
	if creatorIdStr := req.CreatorId; creatorIdStr != "" {
		creatorId, err := strconv.Atoi(creatorIdStr)
		if err == nil {
			request.CreatorId = &creatorId
		}
	}
	if tags := req.Tags; tags != "" {
		request.Tags = &tags
	}

	return request
}
