package mapper

import (
	"encoding/json"
	"jaecoo-be/app/database/entity"
	res "jaecoo-be/app/module/sales_agents/response"
	"path/filepath"
)

func SalesAgentsResponseMapper(agent *entity.SalesAgents, host string) *res.SalesAgentsResponse {
	if agent == nil {
		return nil
	}

	var agentType []string
	if agent.AgentType != nil && *agent.AgentType != "" {
		json.Unmarshal([]byte(*agent.AgentType), &agentType)
	}

	response := &res.SalesAgentsResponse{
		ID:                 agent.ID,
		Name:               agent.Name,
		JobTitle:           agent.JobTitle,
		Phone:              agent.Phone,
		AgentType:          agentType,
		ProfilePicturePath: agent.ProfilePicturePath,
		IsActive:           agent.IsActive,
		CreatedAt:          agent.CreatedAt,
		UpdatedAt:          agent.UpdatedAt,
	}

	if agent.ProfilePicturePath != nil && *agent.ProfilePicturePath != "" {
		// Extract filename from path
		filename := filepath.Base(*agent.ProfilePicturePath)
		profilePictureUrl := host + "/sales-agents/viewer/" + filename
		response.ProfilePictureUrl = &profilePictureUrl
	}

	return response
}
