package mapper

import (
	"narasi-ahli-be/app/database/entity"
	"path/filepath"

	res "narasi-ahli-be/app/module/knowledge_base/response"
)

func KnowledgeBaseResponseMapper(
	kb *entity.KnowledgeBase,
	host string,
) (out *res.KnowledgeBaseResponse) {

	if kb == nil {
		return nil
	}

	viewerBase := host + "/knowledge-base/viewer/"

	var journalUrl, audioUrl, videoUrl *string

	// JOURNAL
	if kb.FileJournalUrl != nil && *kb.FileJournalUrl != "" {
		filename := filepath.Base(*kb.FileJournalUrl)
		url := viewerBase + filename
		journalUrl = &url
	}

	// AUDIO
	if kb.FileAudioUrl != nil && *kb.FileAudioUrl != "" {
		filename := filepath.Base(*kb.FileAudioUrl)
		url := viewerBase + filename
		audioUrl = &url
	}

	// VIDEO
	if kb.FileVideoUrl != nil && *kb.FileVideoUrl != "" {
		filename := filepath.Base(*kb.FileVideoUrl)
		url := viewerBase + filename
		videoUrl = &url
	}

	out = &res.KnowledgeBaseResponse{
		ID:          kb.ID,
		AgentId:     kb.AgentId,
		AgentName:   kb.AgentName,
		CreatedById: kb.CreatedById,
		Title:       kb.Title,
		Status:      kb.Status,

		// 🔥 HANYA viewer URL yang dikirim
		FileJournalUrl: journalUrl,
		FileAudioUrl:   audioUrl,
		FileVideoUrl:   videoUrl,

		IsActive:  kb.IsActive,
		CreatedAt: kb.CreatedAt,
		UpdatedAt: kb.UpdatedAt,
	}

	return out
}
