package request

import (
	"go-humas-be/app/database/entity"
	"go-humas-be/utils/paginator"
	"strconv"
	"time"
)

type ArticlesGeneric interface {
	ToEntity()
}

type ArticlesQueryRequest struct {
	Title       *string               `json:"title"`
	Description *string               `json:"description"`
	CategoryId  *int                  `json:"categoryId"`
	TypeId      *int                  `json:"typeId"`
	Tags        *string               `json:"tags"`
	CreatedById *int                  `json:"createdById"`
	StatusId    *int                  `json:"statusId"`
	IsPublish   *bool                 `json:"isPublish"`
	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"`
	CategoryId      int    `json:"categoryId" validate:"required"`
	TypeId          int    `json:"typeId" validate:"required"`
	Tags            string `json:"tags" validate:"required"`
}

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,
		CategoryId:      req.CategoryId,
	}
}

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"`
	CategoryId      int    `json:"categoryId" validate:"required"`
	TypeId          int    `json:"typeId" validate:"required"`
	Tags            string `json:"tags" validate:"required"`
	StatusId        *int   `json:"statusId"`
}

func (req ArticlesUpdateRequest) ToEntity() *entity.Articles {
	return &entity.Articles{
		Title:           req.Title,
		Slug:            req.Slug,
		Description:     req.Description,
		HtmlDescription: req.HtmlDescription,
		CategoryId:      req.CategoryId,
		TypeId:          req.TypeId,
		Tags:            req.Tags,
		StatusId:        req.StatusId,
		UpdatedAt:       time.Now(),
	}
}

type ArticlesQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	CategoryId  string `json:"categoryId"`
	TypeId      string `json:"typeId"`
	Tags        string `json:"tags"`
	CreatedById string `json:"createdById"`
	IsPublish   string `json:"isPublish"`
	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 categoryIdStr := req.CategoryId; categoryIdStr != "" {
		categoryId, err := strconv.Atoi(categoryIdStr)
		if err == nil {
			request.CategoryId = &categoryId
		}
	}
	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 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
}
