package request

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

type ArticleCategoriesGeneric interface {
	ToEntity()
}

type ArticleCategoriesQueryRequest struct {
	Title           *string               `json:"title"`
	Description     *string               `json:"description"`
	UserLevelId     *uint                 `json:"UserLevelId"`
	UserLevelNumber *int                  `json:"UserLevelNumber"`
	ParentId        *int                  `json:"parentId"`
	StatusId        *int                  `json:"statusId"`
	IsPublish       *bool                 `json:"isPublish"`
	Pagination      *paginator.Pagination `json:"pagination"`
}

type ArticleCategoriesCreateRequest struct {
	Title         string  `json:"title" validate:"required"`
	Description   string  `json:"description" validate:"required"`
	StatusId      int     `json:"statusId" validate:"required"`
	Tags          *string `json:"tags"`
	Slug          *string `json:"slug"`
	CreatedById   *uint   `json:"createdById"`
	ParentId      *int    `json:"parentId"`
	OldCategoryId *uint   `json:"oldCategoryId"`
}

func (req ArticleCategoriesCreateRequest) ToEntity() *entity.ArticleCategories {
	return &entity.ArticleCategories{
		Title:         req.Title,
		Description:   req.Description,
		Tags:          req.Tags,
		ParentId:      req.ParentId,
		Slug:          req.Slug,
		OldCategoryId: req.OldCategoryId,
		StatusId:      req.StatusId,
	}
}

type ArticleCategoriesUpdateRequest struct {
	ID          uint       `json:"id" validate:"required"`
	Title       string     `json:"title" validate:"required"`
	Description string     `json:"description" validate:"required"`
	StatusId    int        `json:"statusId" validate:"required"`
	Tags        *string    `json:"tags"`
	Slug        *string    `json:"slug"`
	ParentId    *int       `json:"parentId"`
	CreatedById *uint      `json:"createdById"`
	IsPublish   *bool      `json:"isPublish"`
	PublishedAt *time.Time `json:"publishedAt"`
}

func (req ArticleCategoriesUpdateRequest) ToEntity() *entity.ArticleCategories {
	return &entity.ArticleCategories{
		ID:          req.ID,
		Title:       req.Title,
		Description: req.Description,
		ParentId:    req.ParentId,
		Slug:        req.Slug,
		Tags:        req.Tags,
		StatusId:    req.StatusId,
		IsPublish:   req.IsPublish,
		PublishedAt: req.PublishedAt,
		UpdatedAt:   time.Now(),
	}
}

type ArticleCategoriesQueryRequestContext struct {
	Title           string `json:"title"`
	Description     string `json:"description"`
	ParentId        string `json:"parentId"`
	StatusId        string `json:"statusId"`
	IsPublish       string `json:"isPublish"`
	UserLevelId     string `json:"UserLevelId"`
	UserLevelNumber string `json:"UserLevelNumber"`
}

func (req ArticleCategoriesQueryRequestContext) ToParamRequest() ArticleCategoriesQueryRequest {
	var request ArticleCategoriesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if parentIdStr := req.ParentId; parentIdStr != "" {
		parentId, err := strconv.Atoi(parentIdStr)
		if err == nil {
			request.ParentId = &parentId
		}
	}
	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 userLevelIdStr := req.UserLevelId; userLevelIdStr != "" {
		userLevelId, err := strconv.Atoi(userLevelIdStr)
		if err == nil {
			userLevelIdUint := uint(userLevelId)
			request.UserLevelId = &userLevelIdUint
		}
	}
	if userLevelNumberStr := req.UserLevelNumber; userLevelNumberStr != "" {
		userLevelNumber, err := strconv.Atoi(userLevelNumberStr)
		if err == nil {
			request.UserLevelNumber = &userLevelNumber
		}
	}

	return request
}
