package mapper

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"netidhub-saas-be/app/database/entity"
	articleCategoriesMapper "netidhub-saas-be/app/module/article_categories/mapper"
	articleCategoriesRepository "netidhub-saas-be/app/module/article_categories/repository"
	articleCategoriesResponse "netidhub-saas-be/app/module/article_categories/response"
	articleCategoryDetailsRepository "netidhub-saas-be/app/module/article_category_details/repository"
	articleFilesMapper "netidhub-saas-be/app/module/article_files/mapper"
	articleFilesRepository "netidhub-saas-be/app/module/article_files/repository"
	articleFilesResponse "netidhub-saas-be/app/module/article_files/response"
	res "netidhub-saas-be/app/module/articles/response"
	usersRepository "netidhub-saas-be/app/module/users/repository"
)

func ArticlesResponseMapper(
	log zerolog.Logger,
	host string,
	clientId *uuid.UUID,
	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(clientId, *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(clientId, 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
}
