// Code scaffolded by goctl. Safe to edit.
// goctl 1.10.1

package dispute

import (
	"context"
	"errors"
	"time"

	"juwan-backend/app/dispute/api/internal/svc"
	"juwan-backend/app/dispute/api/internal/types"
	"juwan-backend/app/dispute/rpc/disputeservice"
	"juwan-backend/app/order/rpc/orderservice"
	"juwan-backend/common/utils/contextj"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateDisputeLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 发起争议
func NewCreateDisputeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDisputeLogic {
	return &CreateDisputeLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateDisputeLogic) CreateDispute(req *types.CreateDisputeReq) (resp *types.EmptyResp, err error) {
	uid, err := contextj.UserIDFrom(l.ctx)
	if err != nil {
		return nil, err
	}
	if req.Reason == "" {
		return nil, errors.New("reason is required")
	}

	orderResp, err := l.svcCtx.OrderRpc.GetOrdersById(l.ctx, &orderservice.GetOrdersByIdReq{Id: req.Id})
	if err != nil {
		return nil, err
	}
	order := orderResp.GetOrders()
	if order == nil {
		return nil, errors.New("order not found")
	}
	if order.GetStatus() != "in_progress" && order.GetStatus() != "pending_close" {
		return nil, errors.New("order status does not allow disputes")
	}

	consumerID, playerUserID, err := orderParticipantUserIDs(l.ctx, l.svcCtx, order)
	if err != nil {
		return nil, err
	}

	var respondentID int64
	if uid == consumerID {
		respondentID = playerUserID
	} else if uid == playerUserID {
		respondentID = consumerID
	} else {
		return nil, errors.New("not a participant of this order")
	}

	existing, err := l.svcCtx.DisputeRpc.SearchDisputes(l.ctx, &disputeservice.SearchDisputesReq{
		Offset:  0,
		Limit:   1,
		OrderId: &req.Id,
	})
	if err != nil {
		return nil, err
	}
	if len(existing.GetDisputes()) > 0 {
		return nil, errors.New("dispute already exists")
	}

	created, err := l.svcCtx.DisputeRpc.AddDisputes(l.ctx, &disputeservice.AddDisputesReq{
		OrderId:       req.Id,
		InitiatorId:   uid,
		InitiatorName: actorName(uid),
		RespondentId:  respondentID,
		Reason:        req.Reason,
		Evidence:      req.Evidence,
		Status:        "open",
	})
	if err != nil {
		return nil, err
	}

	_, err = l.svcCtx.DisputeRpc.AddDisputeTimeline(l.ctx, &disputeservice.AddDisputeTimelineReq{
		DisputeId: created.GetId(),
		EventType: "created",
		ActorId:   uid,
		ActorName: actorName(uid),
		Details:   detailsJSON(map[string]any{"reason": req.Reason, "evidence": req.Evidence}),
	})
	if err != nil {
		return nil, err
	}

	now := time.Now().Unix()
	status := "disputed"
	oldStatus := order.GetStatus()
	metadata := detailsJSON(map[string]any{"disputeId": created.GetId()})
	_, err = l.svcCtx.OrderRpc.UpdateOrders(l.ctx, &orderservice.UpdateOrdersReq{
		Id:        req.Id,
		Status:    &status,
		UpdatedAt: &now,
	})
	if err != nil {
		return nil, err
	}
	_, err = l.svcCtx.OrderRpc.AddOrderStateLogs(l.ctx, &orderservice.AddOrderStateLogsReq{
		OrderId:    req.Id,
		FromStatus: &oldStatus,
		ToStatus:   status,
		Action:     "open_dispute",
		ActorId:    uid,
		ActorRole:  "user",
		Metadata:   &metadata,
		CreatedAt:  &now,
	})
	if err != nil {
		return nil, err
	}

	return &types.EmptyResp{}, nil
}
