package com.ycwl.basic.pricing.dto;

import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * 优惠券领取结果DTO
 */
@Data
public class CouponClaimResult {
    
    /**
     * 是否成功
     */
    private boolean success;
    
    /**
     * 错误消息（失败时）
     */
    private String errorMessage;
    
    /**
     * 错误码（失败时）
     */
    private String errorCode;
    
    /**
     * 领取记录ID（成功时）
     */
    private Long claimRecordId;
    
    /**
     * 优惠券ID
     */
    private Long couponId;
    
    /**
     * 优惠券名称
     */
    private String couponName;
    
    /**
     * 领取时间
     */
    private Date claimTime;
    
    /**
     * 用户ID
     */
    private Long userId;
    
    /**
     * 景区ID
     */
    private String scenicId;
    private PriceCouponConfig coupon;

    /**
     * 已领取的记录列表（领取失败时返回，帮助前端展示用户已有的券）
     */
    private List<PriceCouponClaimRecord> claimedRecords;

    /**
     * 创建成功结果
     */
    public static CouponClaimResult success(PriceCouponClaimRecord record, PriceCouponConfig coupon) {
        CouponClaimResult result = new CouponClaimResult();
        result.coupon = coupon;
        result.success = true;
        result.claimRecordId = record.getId();
        result.couponId = record.getCouponId();
        result.couponName = coupon.getCouponName();
        result.claimTime = record.getClaimTime();
        result.userId = record.getUserId();
        result.scenicId = record.getScenicId();
        return result;
    }
    
    /**
     * 创建失败结果
     */
    public static CouponClaimResult failure(String errorCode, String errorMessage) {
        CouponClaimResult result = new CouponClaimResult();
        result.success = false;
        result.errorCode = errorCode;
        result.errorMessage = errorMessage;
        return result;
    }

    /**
     * 创建失败结果（带已领取的券列表）
     */
    public static CouponClaimResult failureWithClaimedRecords(String errorCode, String errorMessage,
                                                              List<PriceCouponClaimRecord> claimedRecords,
                                                              PriceCouponConfig coupon) {
        CouponClaimResult result = new CouponClaimResult();
        result.success = false;
        result.errorCode = errorCode;
        result.errorMessage = errorMessage;
        result.claimedRecords = claimedRecords;
        result.coupon = coupon;
        result.couponId = coupon != null ? coupon.getId() : null;
        result.couponName = coupon != null ? coupon.getCouponName() : null;
        return result;
    }
    
    /**
     * 创建失败结果（仅错误消息）
     */
    public static CouponClaimResult failure(String errorMessage) {
        return failure("CLAIM_FAILED", errorMessage);
    }
    
    // 常用错误码常量
    public static final String ERROR_COUPON_NOT_FOUND = "COUPON_NOT_FOUND";
    public static final String ERROR_COUPON_EXPIRED = "COUPON_EXPIRED";
    public static final String ERROR_COUPON_INACTIVE = "COUPON_INACTIVE";
    public static final String ERROR_COUPON_OUT_OF_STOCK = "COUPON_OUT_OF_STOCK";
    public static final String ERROR_ALREADY_CLAIMED = "ALREADY_CLAIMED";
    public static final String ERROR_CLAIM_LIMIT_REACHED = "CLAIM_LIMIT_REACHED";
    public static final String ERROR_INVALID_PARAMS = "INVALID_PARAMS";
    public static final String ERROR_SYSTEM_ERROR = "SYSTEM_ERROR";
}