package request

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

type MagazinesGeneric interface {
	ToEntity()
}

type MagazinesQueryRequest struct {
	Title         *string               `json:"title"`
	Description   *string               `json:"description"`
	ThumbnailPath *string               `json:"thumbnailPath"`
	ThumbnailUrl  *string               `json:"thumbnailUrl"`
	PageUrl       *string               `json:"pageUrl"`
	CreatedById   *int                  `json:"createdById"`
	StatusId      *int                  `json:"statusId"`
	IsPublish     *bool                 `json:"isPublish"`
	Pagination    *paginator.Pagination `json:"pagination"`
}

type MagazinesCreateRequest struct {
	Title         string     `json:"title" validate:"required"`
	Description   string     `json:"description" validate:"required"`
	StatusId      int        `json:"statusId" validate:"required"`
	ThumbnailPath *string    `json:"thumbnailPath"`
	ThumbnailUrl  *string    `json:"thumbnailUrl"`
	PageUrl       *string    `json:"pageUrl"`
	CreatedById   *uint      `json:"createdById"`
	IsPublish     *bool      `json:"isPublish"`
	PublishedAt   *time.Time `json:"publishedAt"`
}

func (req MagazinesCreateRequest) ToEntity() *entity.Magazines {
	return &entity.Magazines{
		Title:         req.Title,
		Description:   req.Description,
		ThumbnailPath: req.ThumbnailPath,
		ThumbnailUrl:  req.ThumbnailUrl,
		PageUrl:       req.PageUrl,
		CreatedById:   req.CreatedById,
		StatusId:      req.StatusId,
		IsPublish:     req.IsPublish,
		PublishedAt:   req.PublishedAt,
		IsActive:      true,
	}
}

type MagazinesUpdateRequest 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"`
	ThumbnailPath *string    `json:"thumbnailPath"`
	ThumbnailUrl  *string    `json:"thumbnailUrl"`
	PageUrl       *string    `json:"pageUrl"`
	CreatedById   *uint      `json:"createdById"`
	IsPublish     *bool      `json:"isPublish"`
	PublishedAt   *time.Time `json:"publishedAt"`
}

func (req MagazinesUpdateRequest) ToEntity() *entity.Magazines {
	return &entity.Magazines{
		ID:            req.ID,
		Title:         req.Title,
		Description:   req.Description,
		ThumbnailPath: req.ThumbnailPath,
		ThumbnailUrl:  req.ThumbnailUrl,
		PageUrl:       req.PageUrl,
		CreatedById:   req.CreatedById,
		StatusId:      req.StatusId,
		IsPublish:     req.IsPublish,
		PublishedAt:   req.PublishedAt,
		UpdatedAt:     time.Now(),
	}
}

type MagazinesQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	CreatedById string `json:"createdById"`
	IsPublish   string `json:"isPublish"`
	StatusId    string `json:"statusId"`
}

func (req MagazinesQueryRequestContext) ToParamRequest() MagazinesQueryRequest {
	var request MagazinesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	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
}
