package mapper

import (
	"narasi-ahli-be/app/database/entity"
	articleCategoriesMapper "narasi-ahli-be/app/module/article_categories/mapper"
	articleCategoriesRepository "narasi-ahli-be/app/module/article_categories/repository"
	articleCategoriesResponse "narasi-ahli-be/app/module/article_categories/response"
	articleCategoryDetailsRepository "narasi-ahli-be/app/module/article_category_details/repository"
	articleFilesMapper "narasi-ahli-be/app/module/article_files/mapper"
	articleFilesRepository "narasi-ahli-be/app/module/article_files/repository"
	articleFilesResponse "narasi-ahli-be/app/module/article_files/response"
	res "narasi-ahli-be/app/module/articles/response"
	usersRepository "narasi-ahli-be/app/module/users/repository"

	"github.com/rs/zerolog"
)

func ArticlesResponseMapper(
	log zerolog.Logger,
	host string,
	articlesReq *entity.Articles,
	articleCategoriesRepo articleCategoriesRepository.ArticleCategoriesRepository,
	articleCategoryDetailsRepo articleCategoryDetailsRepository.ArticleCategoryDetailsRepository,
	articleFilesRepo articleFilesRepository.ArticleFilesRepository,
	usersRepo usersRepository.UsersRepository,
) (articlesRes *res.ArticlesResponse) {

	createdByName := ""
	if articlesReq.CreatedById != nil {
		findUser, _ := usersRepo.FindOne(*articlesReq.CreatedById)
		if findUser != nil {
			createdByName = findUser.Fullname
		}
	}

	categoryName := ""
	articleCategories, _ := articleCategoryDetailsRepo.FindByArticleId(articlesReq.ID)
	var articleCategoriesArr []*articleCategoriesResponse.ArticleCategoriesResponse
	if articleCategories != nil && len(articleCategories) > 0 {
		for _, result := range articleCategories {
			articleCategoriesArr = append(articleCategoriesArr, articleCategoriesMapper.ArticleCategoriesResponseMapper(result.Category, host))
		}
		log.Info().Interface("articleCategoriesArr", articleCategoriesArr).Msg("")
	}

	articleFiles, _ := articleFilesRepo.FindByArticle(articlesReq.ID)
	var articleFilesArr []*articleFilesResponse.ArticleFilesResponse
	if articleFiles != nil && len(articleFiles) > 0 {
		for _, result := range articleFiles {
			articleFilesArr = append(articleFilesArr, articleFilesMapper.ArticleFilesResponseMapper(result, host))
		}
	}

	if articlesReq != nil {
		articlesRes = &res.ArticlesResponse{
			ID:                articlesReq.ID,
			Title:             articlesReq.Title,
			Slug:              articlesReq.Slug,
			Description:       articlesReq.Description,
			HtmlDescription:   articlesReq.HtmlDescription,
			TypeId:            articlesReq.TypeId,
			Tags:              articlesReq.Tags,
			CategoryId:        articlesReq.CategoryId,
			AiArticleId:       articlesReq.AiArticleId,
			CategoryName:      categoryName,
			PageUrl:           articlesReq.PageUrl,
			CreatedById:       articlesReq.CreatedById,
			CreatedByName:     &createdByName,
			ShareCount:        articlesReq.ShareCount,
			ViewCount:         articlesReq.ViewCount,
			CommentCount:      articlesReq.CommentCount,
			OldId:             articlesReq.OldId,
			StatusId:          articlesReq.StatusId,
			IsBanner:          articlesReq.IsBanner,
			IsPublish:         articlesReq.IsPublish,
			PublishedAt:       articlesReq.PublishedAt,
			IsActive:          articlesReq.IsActive,
			CreatedAt:         articlesReq.CreatedAt,
			UpdatedAt:         articlesReq.UpdatedAt,
			ArticleFiles:      articleFilesArr,
			ArticleCategories: articleCategoriesArr,
		}

		if articlesReq.ThumbnailName != nil {
			articlesRes.ThumbnailUrl = host + "/articles/thumbnail/viewer/" + *articlesReq.ThumbnailName
		}
	}

	return articlesRes
}
