package order

import (
	"context"
	"encoding/json"

	"github.com/hibiken/asynq"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
	"github.com/perfect-panel/server/pkg/logger"
	"github.com/perfect-panel/server/pkg/xerr"
	"github.com/pkg/errors"
	queue "github.com/perfect-panel/server/queue/types"
)

type ActivateOrderLogic struct {
	logger.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewActivateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ActivateOrderLogic {
	return &ActivateOrderLogic{
		Logger: logger.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ActivateOrderLogic) ActivateOrder(req *types.ActivateOrderRequest) error {
	info, err := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
	if err != nil {
		l.Errorw("[ActivateOrder] FindOneByOrderNo error", logger.Field("error", err.Error()), logger.Field("orderNo", req.OrderNo))
		return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseQueryError), "FindOneByOrderNo error: %v", err.Error())
	}

	// 只允许对 pending(1) 或 closed(3) 的订单手动激活
	if info.Status != 1 && info.Status != 3 {
		l.Infow("[ActivateOrder] order status not eligible",
			logger.Field("orderNo", req.OrderNo),
			logger.Field("status", info.Status),
		)
		return errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "order status %d not eligible for manual activation", info.Status)
	}

	// 强制更新为已支付状态（绕过状态守卫）
	err = l.svcCtx.DB.Model(info).Where("order_no = ?", req.OrderNo).Update("status", 2).Error
	if err != nil {
		l.Errorw("[ActivateOrder] update status to paid failed", logger.Field("error", err.Error()), logger.Field("orderNo", req.OrderNo))
		return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update order status failed: %v", err.Error())
	}

	// enqueue 激活任务
	payload := queue.ForthwithActivateOrderPayload{OrderNo: info.OrderNo}
	p, _ := json.Marshal(payload)
	task := asynq.NewTask(queue.ForthwithActivateOrder, p)
	_, err = l.svcCtx.Queue.EnqueueContext(l.ctx, task)
	if err != nil {
		l.Errorw("[ActivateOrder] enqueue error", logger.Field("error", err.Error()), logger.Field("orderNo", req.OrderNo))
		return errors.Wrapf(xerr.NewErrCode(xerr.QueueEnqueueError), "enqueue error: %v", err.Error())
	}

	l.Infow("[ActivateOrder] order manually activated",
		logger.Field("orderNo", req.OrderNo),
		logger.Field("previousStatus", info.Status),
	)
	return nil
}
