package mapper

import (
	"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"
	clientsRepository "netidhub-saas-be/app/module/clients/repository"
	usersRepository "netidhub-saas-be/app/module/users/repository"

	"github.com/google/uuid"
	"github.com/rs/zerolog"
)

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,
	clientsRepo clientsRepository.ClientsRepository,
) (articlesRes *res.ArticlesResponse) {

	if articlesReq == nil {
		return nil
	}

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

	clientName := ""
	if articlesReq.ClientId != nil {
		findClient, _ := clientsRepo.FindOneByClientId(articlesReq.ClientId)
		if findClient != nil {
			clientName = findClient.Name
		}
	}

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

		// Ambil categoryName dari array pertama
		if len(articleCategoriesArr) > 0 {
			categoryName = articleCategoriesArr[0].Title
		}
	}

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

	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,
		ClientName:        &clientName,
		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
}
