package request

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

type MagazineFilesGeneric interface {
	ToEntity()
}

type MagazineFilesQueryRequest struct {
	Title         *string               `json:"title"`
	Description   *string               `json:"description"`
	MagazineId    *int                  `json:"magazineId"`
	DownloadCount *int                  `json:"downloadCount"`
	StatusId      *int                  `json:"statusId"`
	IsPublish     *bool                 `json:"isPublish"`
	PublishedAt   *time.Time            `json:"publishedAt"`
	IsActive      *bool                 `json:"isActive"`
	Pagination    *paginator.Pagination `json:"pagination"`
}

type MagazineFilesCreateRequest struct {
	Title         string     `json:"title" validate:"required"`
	Description   string     `json:"description" validate:"required"`
	MagazineId    uint       `json:"magazineId" validate:"required"`
	StatusId      int        `json:"statusId" validate:"required"`
	DownloadCount *int       `json:"downloadCount"`
	FilePath      *string    `json:"filePath"`
	FileUrl       *string    `json:"fileUrl"`
	FileName      *string    `json:"fileName"`
	FileAlt       *string    `json:"fileAlt"`
	WidthPixel    *string    `json:"widthPixel"`
	HeightPixel   *string    `json:"heightPixel"`
	Size          *string    `json:"size"`
	IsPublish     *bool      `json:"isPublish"`
	PublishedAt   *time.Time `json:"publishedAt"`
}

func (req MagazineFilesCreateRequest) ToEntity() *entity.MagazineFiles {
	return &entity.MagazineFiles{
		Title:         req.Title,
		Description:   req.Description,
		MagazineId:    req.MagazineId,
		DownloadCount: req.DownloadCount,
		FilePath:      req.FilePath,
		FileUrl:       req.FileUrl,
		FileName:      req.FileName,
		FileAlt:       req.FileAlt,
		WidthPixel:    req.WidthPixel,
		HeightPixel:   req.HeightPixel,
		Size:          req.Size,
		StatusId:      req.StatusId,
		IsPublish:     req.IsPublish,
		PublishedAt:   req.PublishedAt,
	}
}

type MagazineFilesUpdateRequest struct {
	ID            uint       `json:"id" validate:"required"`
	Title         string     `json:"title" validate:"required"`
	Description   string     `json:"description" validate:"required"`
	MagazineId    uint       `json:"magazineId" validate:"required"`
	StatusId      int        `json:"statusId" validate:"required"`
	DownloadCount *int       `json:"downloadCount"`
	FilePath      *string    `json:"filePath"`
	FileUrl       *string    `json:"fileUrl"`
	FileName      *string    `json:"fileName"`
	FileAlt       *string    `json:"fileAlt"`
	WidthPixel    *string    `json:"widthPixel"`
	HeightPixel   *string    `json:"heightPixel"`
	Size          *string    `json:"size"`
	IsPublish     *bool      `json:"isPublish"`
	PublishedAt   *time.Time `json:"publishedAt"`
}

func (req MagazineFilesUpdateRequest) ToEntity() *entity.MagazineFiles {
	return &entity.MagazineFiles{
		ID:            req.ID,
		Title:         req.Title,
		Description:   req.Description,
		MagazineId:    req.MagazineId,
		DownloadCount: req.DownloadCount,
		FilePath:      req.FilePath,
		FileUrl:       req.FileUrl,
		FileName:      req.FileName,
		FileAlt:       req.FileAlt,
		WidthPixel:    req.WidthPixel,
		HeightPixel:   req.HeightPixel,
		Size:          req.Size,
		StatusId:      req.StatusId,
		IsPublish:     req.IsPublish,
		PublishedAt:   req.PublishedAt,
	}
}

type MagazineFilesQueryRequestContext struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	MagazineId  string `json:"magazineId"`
	IsPublish   string `json:"isPublish"`
	StatusId    string `json:"statusId"`
}

func (req MagazineFilesQueryRequestContext) ToParamRequest() MagazineFilesQueryRequest {
	var request MagazineFilesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if description := req.Description; description != "" {
		request.Description = &description
	}
	if magazineIdStr := req.MagazineId; magazineIdStr != "" {
		magazineId, err := strconv.Atoi(magazineIdStr)
		if err == nil {
			request.MagazineId = &magazineId
		}
	}
	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
		}
	}

	return request
}
