package mapper

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/chat/request"
	"campaign-pool-be/app/module/chat/response"
)

// ChatScheduleFileMapper - Mapper for chat schedule file
type ChatScheduleFileMapper struct{}

// NewChatScheduleFileMapper - Create new chat schedule file mapper
func NewChatScheduleFileMapper() *ChatScheduleFileMapper {
	return &ChatScheduleFileMapper{}
}

// ToResponse - Convert entity to response
func (m *ChatScheduleFileMapper) ToResponse(file *entity.ChatScheduleFiles) *response.ChatScheduleFileResponse {
	if file == nil {
		return nil
	}

	return &response.ChatScheduleFileResponse{
		ID:             file.ID,
		ChatScheduleID: file.ChatScheduleID,
		FileName:       file.FileName,
		OriginalName:   file.OriginalName,
		FilePath:       file.FilePath,
		FileSize:       file.FileSize,
		MimeType:       file.MimeType,
		FileType:       file.FileType,
		Description:    file.Description,
		IsRequired:     file.IsRequired,
		CreatedAt:      file.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
		UpdatedAt:      file.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
	}
}

// ToEntity - Convert upload request to entity
func (m *ChatScheduleFileMapper) ToEntity(req request.ChatScheduleFileUploadRequest) *entity.ChatScheduleFiles {
	return &entity.ChatScheduleFiles{
		ChatScheduleID: req.ChatScheduleID,
		FileType:       req.FileType,
		Description:    req.Description,
		IsRequired:     req.IsRequired,
	}
}

// ToUpdateEntity - Convert update request to entity
func (m *ChatScheduleFileMapper) ToUpdateEntity(req request.ChatScheduleFileUpdateRequest) *entity.ChatScheduleFiles {
	file := &entity.ChatScheduleFiles{}

	if req.FileName != "" {
		file.FileName = req.FileName
	}

	if req.OriginalName != "" {
		file.OriginalName = req.OriginalName
	}

	if req.FilePath != "" {
		file.FilePath = req.FilePath
	}

	if req.FileSize > 0 {
		file.FileSize = req.FileSize
	}

	if req.MimeType != "" {
		file.MimeType = req.MimeType
	}

	if req.FileType != "" {
		file.FileType = req.FileType
	}

	if req.Description != "" {
		file.Description = req.Description
	}

	if req.IsRequired != nil {
		file.IsRequired = *req.IsRequired
	}

	return file
}

// ToResponseList - Convert entity list to response list
func (m *ChatScheduleFileMapper) ToResponseList(files []*entity.ChatScheduleFiles) []*response.ChatScheduleFileResponse {
	if files == nil {
		return nil
	}

	responses := make([]*response.ChatScheduleFileResponse, len(files))
	for i, file := range files {
		responses[i] = m.ToResponse(file)
	}

	return responses
}
