package api

import (
	"cs-agent/internal/builders"
	"cs-agent/internal/pkg/dto/request"
	"cs-agent/internal/pkg/enums"
	"cs-agent/internal/pkg/httpx"
	"cs-agent/internal/services"
	"strconv"
	"strings"

	"cs-agent/internal/pkg/httpx/params"

	"github.com/gin-gonic/gin"
	"github.com/mlogclub/simple/web"
	"github.com/spf13/cast"
)

func MessageAnyList(ctx *gin.Context) {
	if services.ChannelService.GetEnabledChannel(ctx) == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
		return
	}
	external := httpx.GetExternalUser(ctx)
	if external == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
		return
	}

	conversationID, _ := params.GetInt64(ctx, "conversationId")
	if conversationID <= 0 {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
		return
	}
	conversation := services.ConversationService.Get(conversationID)
	if conversation == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
		return
	}
	if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
		return
	}

	var (
		senderType, _  = params.Get(ctx, "senderType")
		messageType, _ = params.Get(ctx, "messageType")
		cursor, _      = params.GetInt64(ctx, "cursor")
		limit, _       = params.GetInt(ctx, "limit")
	)
	list, nextCursor, hasMore := services.MessageService.FindByConversationIDCursor(
		conversationID, cursor, limit, senderType, messageType,
	)
	results := builders.BuildMessages(list)
	httpx.WriteJSON(ctx, httpx.CursorData(results, cast.ToString(nextCursor), hasMore))
	return
}

func MessagePostSend(ctx *gin.Context) {
	if services.ChannelService.GetEnabledChannel(ctx) == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
		return
	}
	external := httpx.GetExternalUser(ctx)
	if external == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
		return
	}

	req := request.SendConversationMessageRequest{}
	if err := params.ReadJSON(ctx, &req); err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}

	item, err := services.MessageService.SendCustomerMessage(req.ConversationID, req.ClientMsgID, req.MessageType, req.Content, req.Payload, *external)
	if err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}
	httpx.WriteJSON(ctx, builders.BuildMessage(item))
	return
}

func MessagePostRead(ctx *gin.Context) {
	if services.ChannelService.GetEnabledChannel(ctx) == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
		return
	}
	external := httpx.GetExternalUser(ctx)
	if external == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
		return
	}

	req := request.ReadConversationRequest{}
	if err := params.ReadJSON(ctx, &req); err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}
	if err := services.ConversationService.MarkCustomerConversationReadToMessage(req.ConversationID, req.MessageID, external); err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}
	httpx.WriteJSON(ctx, nil)
	return
}

func MessagePostUpload_image(ctx *gin.Context) {
	if services.ChannelService.GetEnabledChannel(ctx) == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
		return
	}
	external := httpx.GetExternalUser(ctx)
	if external == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
		return
	}

	rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
	if rawConv == "" {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
		return
	}
	conversationID, err := strconv.ParseInt(rawConv, 10, 64)
	if err != nil || conversationID <= 0 {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
		return
	}
	conversation := services.ConversationService.Get(conversationID)
	if conversation == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("会话不存在"))
		return
	}
	if !services.ConversationService.IsCustomerConversationOwner(conversation, *external) {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("无权访问该会话"))
		return
	}
	if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}

	header, err := ctx.FormFile("file")
	if err != nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传图片"))
		return
	}
	if !strings.HasPrefix(strings.ToLower(header.Header.Get("Content-Type")), "image/") {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("仅支持上传图片文件"))
		return
	}

	item, err := services.AssetService.UploadFile(header, "images", nil)
	if err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}
	httpx.WriteJSON(ctx, builders.BuildAsset(item))
	return
}

func MessagePostUpload_attachment(ctx *gin.Context) {
	if services.ChannelService.GetEnabledChannel(ctx) == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("接入渠道未初始化"))
		return
	}
	external := httpx.GetExternalUser(ctx)
	if external == nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("外部身份未初始化"))
		return
	}

	rawConv := strings.TrimSpace(params.FormValue(ctx, "conversationId"))
	if rawConv == "" {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
		return
	}
	conversationID, err := strconv.ParseInt(rawConv, 10, 64)
	if err != nil || conversationID <= 0 {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("conversationId不能为空"))
		return
	}
	if _, err := services.MessageService.ValidateConversationSender(conversationID, enums.IMSenderTypeCustomer, nil, external); err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}

	header, err := ctx.FormFile("file")
	if err != nil {
		httpx.WriteJSON(ctx, web.JsonErrorMsg("请选择上传附件"))
		return
	}
	item, err := services.AssetService.UploadFile(header, "attachments", nil)
	if err != nil {
		httpx.WriteJSON(ctx, err)
		return
	}
	httpx.WriteJSON(ctx, builders.BuildAsset(item))
	return
}
