package com.gxwebsoft.shop.service;

import com.gxwebsoft.shop.entity.ShopUserCoupon;

import java.util.List;

/**
 * 优惠券状态管理服务
 *
 * @author WebSoft
 * @since 2025-01-15
 */
public interface CouponStatusService {

    /**
     * 获取用户可用的优惠券列表
     *
     * @param userId 用户ID
     * @return 可用优惠券列表
     */
    List<ShopUserCoupon> getAvailableCoupons(Integer userId);

    /**
     * 获取用户已使用的优惠券列表
     *
     * @param userId 用户ID
     * @return 已使用优惠券列表
     */
    List<ShopUserCoupon> getUsedCoupons(Integer userId);

    /**
     * 获取用户已过期的优惠券列表
     *
     * @param userId 用户ID
     * @return 已过期优惠券列表
     */
    List<ShopUserCoupon> getExpiredCoupons(Integer userId);

    /**
     * 获取用户所有优惠券并按状态分类
     *
     * @param userId 用户ID
     * @return 分类后的优惠券列表
     */
    CouponStatusResult getUserCouponsGroupByStatus(Integer userId);

    /**
     * 使用优惠券
     *
     * @param userCouponId 用户优惠券ID
     * @param orderId      订单ID
     * @param orderNo      订单号
     * @return 是否成功
     */
    boolean useCoupon(Long userCouponId, Integer orderId, String orderNo);

    /**
     * 退还优惠券（订单取消时）
     *
     * @param orderId 订单ID
     * @return 是否成功
     */
    boolean returnCoupon(Integer orderId);

    /**
     * 批量更新过期优惠券状态
     *
     * @return 更新的数量
     */
    int updateExpiredCoupons();

    /**
     * 检查并更新单个优惠券状态
     *
     * @param userCoupon 用户优惠券
     * @return 是否状态发生变化
     */
    boolean checkAndUpdateCouponStatus(ShopUserCoupon userCoupon);

    /**
     * 验证优惠券是否可用于指定订单
     *
     * @param userCouponId 用户优惠券ID
     * @param totalAmount  订单总金额
     * @param goodsIds     商品ID列表
     * @return 验证结果
     */
    CouponValidationResult validateCouponForOrder(Long userCouponId, 
                                                   java.math.BigDecimal totalAmount, 
                                                   List<Integer> goodsIds);

    /**
     * 优惠券状态分类结果
     */
    class CouponStatusResult {
        private List<ShopUserCoupon> availableCoupons;  // 可用优惠券
        private List<ShopUserCoupon> usedCoupons;       // 已使用优惠券
        private List<ShopUserCoupon> expiredCoupons;    // 已过期优惠券
        private int totalCount;                         // 总数量

        // 构造函数
        public CouponStatusResult(List<ShopUserCoupon> availableCoupons,
                                  List<ShopUserCoupon> usedCoupons,
                                  List<ShopUserCoupon> expiredCoupons) {
            this.availableCoupons = availableCoupons;
            this.usedCoupons = usedCoupons;
            this.expiredCoupons = expiredCoupons;
            this.totalCount = availableCoupons.size() + usedCoupons.size() + expiredCoupons.size();
        }

        // Getters and Setters
        public List<ShopUserCoupon> getAvailableCoupons() { return availableCoupons; }
        public void setAvailableCoupons(List<ShopUserCoupon> availableCoupons) { this.availableCoupons = availableCoupons; }
        
        public List<ShopUserCoupon> getUsedCoupons() { return usedCoupons; }
        public void setUsedCoupons(List<ShopUserCoupon> usedCoupons) { this.usedCoupons = usedCoupons; }
        
        public List<ShopUserCoupon> getExpiredCoupons() { return expiredCoupons; }
        public void setExpiredCoupons(List<ShopUserCoupon> expiredCoupons) { this.expiredCoupons = expiredCoupons; }
        
        public int getTotalCount() { return totalCount; }
        public void setTotalCount(int totalCount) { this.totalCount = totalCount; }
    }

    /**
     * 优惠券验证结果
     */
    class CouponValidationResult {
        private boolean valid;              // 是否有效
        private String message;             // 验证消息
        private java.math.BigDecimal discountAmount; // 优惠金额

        public CouponValidationResult(boolean valid, String message) {
            this.valid = valid;
            this.message = message;
        }

        public CouponValidationResult(boolean valid, String message, java.math.BigDecimal discountAmount) {
            this.valid = valid;
            this.message = message;
            this.discountAmount = discountAmount;
        }

        // Getters and Setters
        public boolean isValid() { return valid; }
        public void setValid(boolean valid) { this.valid = valid; }
        
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        
        public java.math.BigDecimal getDiscountAmount() { return discountAmount; }
        public void setDiscountAmount(java.math.BigDecimal discountAmount) { this.discountAmount = discountAmount; }
    }
}
