package com.jeesharp.business.modules.member.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 积分发放请求，供跨模块 {@link com.jeesharp.business.modules.member.api.MemberPointsApi} 使用。
 *
 * @author jeesharp
 * @since 2026-06-23
 */
@Data
@Builder
@Schema(description = "积分发放请求")
public class MemberPointsGrantDTO {

    @Schema(description = "会员ID", required = true)
    private String memberId;

    @Schema(description = "场景编码", required = true)
    private String sceneCode;

    @Schema(description = "业务幂等键", required = true)
    private String bizId;

    @Schema(description = "业务编号")
    private String bizNo;

    @Schema(description = "业务描述")
    private String bizDesc;

    @Schema(description = "操作来源：SYSTEM/ADMIN/APP/MINI")
    private String operateSource;

    @Builder.Default
    @Schema(description = "扩展上下文：payAmount、continuousDays 等")
    private Map<String, Object> context = new HashMap<>();

    @Schema(description = "指定发放积分（覆盖规则计算，用于人工调账、佣金等固定分值场景）")
    private Integer overridePoints;

    public BigDecimal payAmount() {
        Object v = context != null ? context.get("payAmount") : null;
        if (v instanceof BigDecimal bd) {
            return bd;
        }
        if (v instanceof Number n) {
            return BigDecimal.valueOf(n.doubleValue());
        }
        if (v instanceof String s && !s.isBlank()) {
            return new BigDecimal(s.trim());
        }
        return null;
    }

    public Integer continuousDays() {
        Object v = context != null ? context.get("continuousDays") : null;
        if (v instanceof Number n) {
            return n.intValue();
        }
        if (v instanceof String s && !s.isBlank()) {
            return Integer.parseInt(s.trim());
        }
        return null;
    }
}
