package common

import (
	"strings"

	ordermodel "github.com/perfect-panel/server/internal/model/order"
	usermodel "github.com/perfect-panel/server/internal/model/user"
	"github.com/perfect-panel/server/pkg/logger"
)

const (
	SubscriptionTraceType          = "subscription_flow"
	SubscriptionTraceFlowOrder     = "order_subscription"
	SubscriptionTraceFlowEmailBind = "email_bind_subscription"
)

func SubscriptionTraceFields(flow string, stage string, fields ...logger.LogField) []logger.LogField {
	base := []logger.LogField{
		logger.Field("trace_type", SubscriptionTraceType),
		logger.Field("flow", flow),
		logger.Field("stage", stage),
	}

	return append(base, fields...)
}

func SubscriptionTraceInfo(log logger.Logger, flow string, stage string, msg string, fields ...logger.LogField) {
	log.Infow(msg, SubscriptionTraceFields(flow, stage, fields...)...)
}

func SubscriptionTraceError(log logger.Logger, flow string, stage string, msg string, fields ...logger.LogField) {
	log.Errorw(msg, SubscriptionTraceFields(flow, stage, fields...)...)
}

func OrderTraceFields(orderInfo *ordermodel.Order) []logger.LogField {
	if orderInfo == nil {
		return nil
	}

	effectiveUserID := orderInfo.UserId
	if orderInfo.SubscriptionUserId > 0 {
		effectiveUserID = orderInfo.SubscriptionUserId
	}

	fields := []logger.LogField{
		logger.Field("order_id", orderInfo.Id),
		logger.Field("order_no", orderInfo.OrderNo),
		logger.Field("order_type", orderInfo.Type),
		logger.Field("order_status", orderInfo.Status),
		logger.Field("user_id", orderInfo.UserId),
		logger.Field("subscription_user_id", orderInfo.SubscriptionUserId),
		logger.Field("effective_user_id", effectiveUserID),
		logger.Field("order_subscribe_id", orderInfo.SubscribeId),
		logger.Field("payment_id", orderInfo.PaymentId),
		logger.Field("payment_method", orderInfo.Method),
		logger.Field("parent_order_id", orderInfo.ParentId),
		logger.Field("quantity", orderInfo.Quantity),
		logger.Field("is_new_order", orderInfo.IsNew),
	}

	if tail := SensitiveTail(orderInfo.SubscribeToken); tail != "" {
		fields = append(fields, logger.Field("subscribe_token_tail", tail))
	}
	if tail := SensitiveTail(orderInfo.TradeNo); tail != "" {
		fields = append(fields, logger.Field("trade_no_tail", tail))
	}
	if tail := SensitiveTail(orderInfo.AppAccountToken); tail != "" {
		fields = append(fields, logger.Field("app_account_token_tail", tail))
	}

	return fields
}

func UserSubscribeTraceFields(userSub *usermodel.Subscribe) []logger.LogField {
	if userSub == nil {
		return nil
	}

	fields := []logger.LogField{
		logger.Field("user_subscribe_id", userSub.Id),
		logger.Field("subscribe_owner_user_id", userSub.UserId),
		logger.Field("user_subscribe_plan_id", userSub.SubscribeId),
		logger.Field("subscribe_order_id", userSub.OrderId),
		logger.Field("subscribe_status", userSub.Status),
		logger.Field("expire_time", userSub.ExpireTime),
	}

	if tail := SensitiveTail(userSub.Token); tail != "" {
		fields = append(fields, logger.Field("subscribe_token_tail", tail))
	}
	if tail := SensitiveTail(userSub.UUID); tail != "" {
		fields = append(fields, logger.Field("subscribe_uuid_tail", tail))
	}

	return fields
}

func SensitiveTail(value string) string {
	value = strings.TrimSpace(value)
	if value == "" {
		return ""
	}
	if len(value) <= 8 {
		return value
	}

	return value[len(value)-8:]
}
