package mapper

import (
	"campaign-pool-be/app/database/entity"
	magazineFilesMapper "campaign-pool-be/app/module/magazine_files/mapper"
	magazineFilesRepository "campaign-pool-be/app/module/magazine_files/repository"
	magazineFilesResponse "campaign-pool-be/app/module/magazine_files/response"
	res "campaign-pool-be/app/module/magazines/response"
)

func MagazinesResponseMapper(magazinesReq *entity.Magazines, magazineFilesRepo magazineFilesRepository.MagazineFilesRepository, host string) (magazinesRes *res.MagazinesResponse) {
	magazineFiles, _ := magazineFilesRepo.FindByMagazine(magazinesReq.ID)
	var magazineFilesArr []*magazineFilesResponse.MagazineFilesResponse
	if magazineFiles != nil && len(magazineFiles) > 0 {
		for _, result := range magazineFiles {
			magazineFilesArr = append(magazineFilesArr, magazineFilesMapper.MagazineFilesResponseMapper(result, host))
		}
	}

	if magazinesReq != nil {
		magazinesRes = &res.MagazinesResponse{
			ID:            magazinesReq.ID,
			Title:         magazinesReq.Title,
			Description:   magazinesReq.Description,
			ThumbnailPath: magazinesReq.ThumbnailPath,
			PageUrl:       magazinesReq.PageUrl,
			CreatedById:   magazinesReq.CreatedById,
			StatusId:      magazinesReq.StatusId,
			IsPublish:     magazinesReq.IsPublish,
			PublishedAt:   magazinesReq.PublishedAt,
			IsActive:      magazinesReq.IsActive,
			CreatedAt:     magazinesReq.CreatedAt,
			UpdatedAt:     magazinesReq.UpdatedAt,
			MagazineFiles: magazineFilesArr,
		}

		if magazinesReq.ThumbnailPath != nil {
			magazinesRes.ThumbnailUrl = host + "/magazines/thumbnail/viewer/" + *magazinesReq.ThumbnailName
		}
	}
	return magazinesRes
}
