package request

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

type ArticleFilesGeneric interface {
	ToEntity()
}

type ArticleFilesQueryRequest struct {
	ArticleId  *int                  `json:"articleId"`
	FileName   *string               `json:"fileName"`
	StatusId   *int                  `json:"statusId"`
	IsPublish  *bool                 `json:"isPublish"`
	Pagination *paginator.Pagination `json:"pagination"`
}

type ArticleFilesCreateRequest struct {
	ArticleId     uint   `json:"articleId" validate:"required"`
	FilePath      string `json:"filePath" validate:"required"`
	FileUrl       string `json:"fileUrl" validate:"required"`
	FileName      string `json:"fileName" validate:"required"`
	FileThumbnail string `json:"fileThumbnail" validate:"required"`
	FileAlt       string `json:"fileAlt" validate:"required"`
	WidthPixel    string `json:"widthPixel" validate:"required"`
	HeightPixel   string `json:"heightPixel" validate:"required"`
	Size          string `json:"size" validate:"required"`
	DownloadCount int    `json:"downloadCount" validate:"required"`
	StatusId      int    `json:"statusId" validate:"required"`
}

func (req ArticleFilesCreateRequest) ToEntity() *entity.ArticleFiles {
	return &entity.ArticleFiles{
		ArticleId:     req.ArticleId,
		FilePath:      req.FilePath,
		FileUrl:       req.FileUrl,
		FileName:      req.FileName,
		FileThumbnail: req.FileThumbnail,
		FileAlt:       req.FileAlt,
		WidthPixel:    req.WidthPixel,
		HeightPixel:   req.HeightPixel,
		Size:          req.Size,
		DownloadCount: req.DownloadCount,
		StatusId:      req.StatusId,
		IsPublish:     false,
	}
}

type ArticleFilesUpdateRequest struct {
	ID            uint      `json:"id" validate:"required"`
	ArticleId     uint      `json:"articleId" validate:"required"`
	FilePath      string    `json:"filePath" validate:"required"`
	FileUrl       string    `json:"fileUrl" validate:"required"`
	FileName      string    `json:"fileName" validate:"required"`
	FileThumbnail string    `json:"fileThumbnail" validate:"required"`
	FileAlt       string    `json:"fileAlt" validate:"required"`
	WidthPixel    string    `json:"widthPixel" validate:"required"`
	HeightPixel   string    `json:"heightPixel" validate:"required"`
	Size          string    `json:"size" validate:"required"`
	DownloadCount int       `json:"downloadCount" validate:"required"`
	CreatedById   int       `json:"createdById" validate:"required"`
	StatusId      int       `json:"statusId" validate:"required"`
	IsPublish     bool      `json:"isPublish" validate:"required"`
	PublishedAt   time.Time `json:"publishedAt" validate:"required"`
}

func (req ArticleFilesUpdateRequest) ToEntity() *entity.ArticleFiles {
	return &entity.ArticleFiles{
		ID:            req.ID,
		ArticleId:     req.ArticleId,
		FilePath:      req.FilePath,
		FileUrl:       req.FileUrl,
		FileName:      req.FileName,
		FileThumbnail: req.FileThumbnail,
		FileAlt:       req.FileAlt,
		WidthPixel:    req.WidthPixel,
		HeightPixel:   req.HeightPixel,
		Size:          req.Size,
		DownloadCount: req.DownloadCount,
		CreatedById:   req.CreatedById,
		StatusId:      req.StatusId,
		IsPublish:     req.IsPublish,
		PublishedAt:   req.PublishedAt,
		UpdatedAt:     time.Now(),
	}
}

type ArticleFilesQueryRequestContext struct {
	ArticleId string `json:"articleId"`
	FileName  string `json:"fileName"`
	StatusId  string `json:"statusId"`
	IsPublish string `json:"isPublish"`
}

func (req ArticleFilesQueryRequestContext) ToParamRequest() ArticleFilesQueryRequest {
	var request ArticleFilesQueryRequest

	if articleIdStr := req.ArticleId; articleIdStr != "" {
		articleId, err := strconv.Atoi(articleIdStr)
		if err == nil {
			request.ArticleId = &articleId
		}
	}
	if fileName := req.FileName; fileName != "" {
		request.FileName = &fileName
	}
	if statusIdStr := req.StatusId; statusIdStr != "" {
		statusId, err := strconv.Atoi(statusIdStr)
		if err == nil {
			request.StatusId = &statusId
		}
	}
	if isPublishStr := req.IsPublish; isPublishStr != "" {
		isPublish, err := strconv.ParseBool(isPublishStr)
		if err == nil {
			request.IsPublish = &isPublish
		}
	}

	return request
}
