package request

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

type PpidDataFilesGeneric interface {
	ToEntity()
}

type PpidDataFilesQueryRequest struct {
	Title       *string               `json:"title"`
	PpidDataId  *int                  `json:"ppidDataId"`
	Description *string               `json:"description"`
	Unit        *string               `json:"unit"`
	FileType    *string               `json:"fileType"`
	StatusId    *int                  `json:"statusId"`
	IsPublish   *bool                 `json:"isPublish"`
	Pagination  *paginator.Pagination `json:"pagination"`
}

type PpidDataFilesCreateRequest struct {
	PpidDataId  int     `json:"ppidDataId" validate:"required"`
	Title       *string `json:"title" validate:"required"`
	FileType    *string `json:"fileType"`
	FileName    *string `json:"fileName"`
	FilePath    *string `json:"filePath"`
	Size        *string `json:"size"`
	CreatedById *int    `json:"createdById"`
	StatusId    *int    `json:"statusId"`
}

func (req PpidDataFilesCreateRequest) ToEntity() *entity.PpidDataFiles {
	return &entity.PpidDataFiles{
		Title:       req.Title,
		PpidDataId:  req.PpidDataId,
		FileType:    req.FileType,
		FileName:    req.FileName,
		FilePath:    req.FilePath,
		Size:        req.Size,
		CreatedById: req.CreatedById,
		StatusId:    req.StatusId,
	}
}

type PpidDataFilesUpdateRequest struct {
	ID          uint       `json:"id" validate:"required"`
	PpidDataId  int        `json:"ppidDataId" validate:"required"`
	Title       *string    `json:"title"`
	FileType    *string    `json:"fileType"`
	FilePath    *string    `json:"filePath"`
	CreatedById *int       `json:"createdById"`
	StatusId    *int       `json:"statusId"`
	IsPublish   *bool      `json:"isPublish"`
	PublishedAt *time.Time `json:"publishedAt"`
}

func (req PpidDataFilesUpdateRequest) ToEntity() *entity.PpidDataFiles {
	return &entity.PpidDataFiles{
		ID:          req.ID,
		Title:       req.Title,
		PpidDataId:  req.PpidDataId,
		FileType:    req.FileType,
		FilePath:    req.FilePath,
		CreatedById: req.CreatedById,
		StatusId:    req.StatusId,
		IsPublish:   req.IsPublish,
		PublishedAt: req.PublishedAt,
		UpdatedAt:   time.Now(),
	}
}

type PpidDataFilesQueryRequestContext struct {
	Title       string `json:"title"`
	PpidDataId  string `json:"ppidDataId"`
	Description string `json:"description"`
	Unit        string `json:"unit"`
	FileType    string `json:"fileType"`
	StatusId    string `json:"statusId"`
	IsPublish   string `json:"isPublish"`
}

func (req PpidDataFilesQueryRequestContext) ToParamRequest() PpidDataFilesQueryRequest {
	var request PpidDataFilesQueryRequest

	if title := req.Title; title != "" {
		request.Title = &title
	}
	if ppidDataIdStr := req.PpidDataId; ppidDataIdStr != "" {
		ppidDataId, err := strconv.Atoi(ppidDataIdStr)
		if err == nil {
			request.PpidDataId = &ppidDataId
		}
	}
	if description := req.Description; description != "" {
		request.Title = &description
	}
	if unit := req.Unit; unit != "" {
		request.Unit = &unit
	}
	if fileType := req.FileType; fileType != "" {
		request.FileType = &fileType
	}
	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
}
