package request

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

type PpidDatasGeneric interface {
	ToEntity()
}

type PpidDatasQueryRequest struct {
	Title        *string               `json:"title"`
	Description  *string               `json:"description"`
	CategoryId   *int                  `json:"categoryId"`
	LevelGroupId *int                  `json:"levelGroupId"`
	Group        *string               `json:"group"`
	CreatedById  *int                  `json:"createdById"`
	StatusId     *int                  `json:"statusId"`
	IsPublish    *bool                 `json:"isPublish"`
	Pagination   *paginator.Pagination `json:"pagination"`
}

type PpidDatasCreateRequest struct {
	Title        string  `json:"title" validate:"required"`
	Description  string  `json:"description" validate:"required"`
	Slug         string  `json:"slug" validate:"required"`
	CategoryId   uint    `json:"categoryId" validate:"required"`
	StatusId     int     `json:"statusId" validate:"required"`
	LevelGroupId *int    `json:"levelGroupId"`
	Group        *string `json:"group"`
}

func (req PpidDatasCreateRequest) ToEntity() *entity.PpidDatas {
	return &entity.PpidDatas{
		Title:        req.Title,
		Description:  req.Description,
		Slug:         req.Slug,
		CategoryId:   req.CategoryId,
		StatusId:     req.StatusId,
		LevelGroupId: req.LevelGroupId,
		Group:        req.Group,
	}
}

type PpidDatasUpdateRequest struct {
	ID                        uint       `json:"id" validate:"required"`
	Title                     string     `json:"title" validate:"required"`
	Description               string     `json:"description" validate:"required"`
	Slug                      string     `json:"slug" validate:"required"`
	CategoryId                uint       `json:"categoryId" validate:"required"`
	StatusId                  int        `json:"statusId" validate:"required"`
	NeedApprovalFromUserRole  *string    `json:"needApprovalFrom"`
	NeedApprovalFromUserLevel *string    `json:"needApprovalFromUserLevel"`
	BackApprovalToUserRole    *int       `json:"backApprovalToUserRole"`
	BackApprovalToUserLevel   *int       `json:"backApprovalToUserLevel"`
	IsPublish                 *bool      `json:"isPublish"`
	PublishLevel              *int       `json:"publishLevel"`
	PublishedAt               *time.Time `json:"publishedAt"`
	LevelGroupId              *int       `json:"levelGroupId"`
	Group                     *string    `json:"group"`
}

func (req PpidDatasUpdateRequest) ToEntity() *entity.PpidDatas {
	return &entity.PpidDatas{
		ID:                        req.ID,
		Title:                     req.Title,
		Description:               req.Description,
		Slug:                      req.Slug,
		CategoryId:                req.CategoryId,
		StatusId:                  req.StatusId,
		NeedApprovalFromUserRole:  req.NeedApprovalFromUserRole,
		NeedApprovalFromUserLevel: req.NeedApprovalFromUserLevel,
		BackApprovalToUserRole:    req.BackApprovalToUserRole,
		BackApprovalToUserLevel:   req.BackApprovalToUserLevel,
		IsPublish:                 req.IsPublish,
		PublishedAt:               req.PublishedAt,
		LevelGroupId:              req.LevelGroupId,
		Group:                     req.Group,
		UpdatedAt:                 time.Now(),
	}
}

type PpidDatasQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	CategoryId  string `json:"categoryId"`
	Group       string `json:"group"`
	CreatedById string `json:"createdById"`
	StatusId    string `json:"statusId"`
	IsPublish   string `json:"isPublish"`
}

func (req PpidDatasQueryRequestContext) ToParamRequest() PpidDatasQueryRequest {
	var request PpidDatasQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if group := req.Group; group != "" {
		request.Group = &group
	}
	if categoryIdStr := req.CategoryId; categoryIdStr != "" {
		categoryId, err := strconv.Atoi(categoryIdStr)
		if err == nil {
			request.CategoryId = &categoryId
		}
	}
	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
}
