package com.ycwl.basic.service;

import com.ycwl.basic.model.pc.notify.entity.UserNotificationAuthorizationEntity;

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

/**
 * 用户通知授权记录Service接口
 * 
 * @Author: System
 * @Date: 2024/12/28
 */
public interface UserNotificationAuthorizationService {
    
    /**
     * 检查用户是否对指定模板有授权
     * 
     * @param memberId 用户ID
     * @param templateId 模板ID
     * @param scenicId 景区ID
     * @return 授权记录，如果没有授权返回null
     */
    UserNotificationAuthorizationEntity checkAuthorization(Long memberId, String templateId, Long scenicId);
    
    /**
     * 检查用户是否还有剩余授权次数
     * 
     * @param memberId 用户ID
     * @param templateId 模板ID
     * @param scenicId 景区ID
     * @return 剩余授权次数，0表示没有剩余
     */
    Integer getRemainingCount(Long memberId, String templateId, Long scenicId);
    
    /**
     * 记录用户授权
     * 
     * @param memberId 用户ID
     * @param templateId 模板ID
     * @param scenicId 景区ID
     * @return 授权记录
     */
    UserNotificationAuthorizationEntity recordAuthorization(Long memberId, String templateId, Long scenicId);

    /**
     * 记录用户授权（支持幂等）
     *
     * @param requestId 前端幂等ID（同一次用户授权动作复用）；为空则不做幂等控制
     */
    UserNotificationAuthorizationEntity recordAuthorization(Long memberId, String templateId, Long scenicId, String requestId);
    
    /**
     * 批量记录用户授权
     * 
     * @param memberId 用户ID
     * @param templateIds 模板ID列表
     * @param scenicId 景区ID
     * @return 批量授权记录结果
     */
    List<AuthorizationRecord> batchRecordAuthorization(Long memberId, List<String> templateIds, Long scenicId);

    /**
     * 批量记录用户授权（支持幂等）
     *
     * @param requestId 前端幂等ID（同一次用户授权动作复用）；为空则不做幂等控制
     */
    List<AuthorizationRecord> batchRecordAuthorization(Long memberId, List<String> templateIds, Long scenicId, String requestId);
    
    /**
     * 消费一次授权
     * 
     * @param memberId 用户ID
     * @param templateId 模板ID
     * @param scenicId 景区ID
     * @return 是否消费成功
     */
    boolean consumeAuthorization(Long memberId, String templateId, Long scenicId);
    
    /**
     * 查询用户的授权记录列表
     * 
     * @param memberId 用户ID
     * @return 授权记录列表
     */
    List<UserNotificationAuthorizationEntity> getUserAuthorizations(Long memberId);
    
    /**
     * 查询用户对指定模板的授权记录列表
     * 
     * @param memberId 用户ID
     * @param templateId 模板ID
     * @return 授权记录列表
     */
    List<UserNotificationAuthorizationEntity> getUserAuthorizationsByTemplate(Long memberId, String templateId);
    
    /**
     * 查询景区的授权记录列表
     * 
     * @param scenicId 景区ID
     * @return 授权记录列表
     */
    List<UserNotificationAuthorizationEntity> getScenicAuthorizations(Long scenicId);
    
    /**
     * 根据ID查询授权记录
     * 
     * @param id 记录ID
     * @return 授权记录
     */
    UserNotificationAuthorizationEntity getById(Long id);
    
    /**
     * 批量检查多个模板的授权状态
     * 
     * @param memberId 用户ID
     * @param templateIds 模板ID列表
     * @param scenicId 景区ID
     * @return 授权记录映射
     */
    Map<String, UserNotificationAuthorizationEntity> batchCheckAuthorization(
            Long memberId, List<String> templateIds, Long scenicId);
    
    /**
     * 获取用户授权统计信息
     * 
     * @param memberId 用户ID
     * @return 统计信息
     */
    AuthorizationStats getAuthorizationStats(Long memberId);
    
    /**
     * 授权记录结果
     */
    class AuthorizationRecord {
        private boolean success;         // 是否成功
        private String templateId;       // 模板ID
        private Long id;                 // 记录ID
        private Long scenicId;           // 景区ID
        private Integer authorizationCount; // 授权次数
        private Integer consumedCount;   // 已消费次数
        private Integer remainingCount;  // 剩余次数
        private Date lastAuthorizedTime; // 最后授权时间
        private Date lastConsumedTime;   // 最后消费时间
        private Integer status;          // 状态
        private Date createTime;         // 创建时间
        private String failureReason;    // 失败原因
        
        // getters and setters
        public boolean isSuccess() { return success; }
        public void setSuccess(boolean success) { this.success = success; }
        public String getTemplateId() { return templateId; }
        public void setTemplateId(String templateId) { this.templateId = templateId; }
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        public Long getScenicId() { return scenicId; }
        public void setScenicId(Long scenicId) { this.scenicId = scenicId; }
        public Integer getAuthorizationCount() { return authorizationCount; }
        public void setAuthorizationCount(Integer authorizationCount) { this.authorizationCount = authorizationCount; }
        public Integer getConsumedCount() { return consumedCount; }
        public void setConsumedCount(Integer consumedCount) { this.consumedCount = consumedCount; }
        public Integer getRemainingCount() { return remainingCount; }
        public void setRemainingCount(Integer remainingCount) { this.remainingCount = remainingCount; }
        public Date getLastAuthorizedTime() { return lastAuthorizedTime; }
        public void setLastAuthorizedTime(Date lastAuthorizedTime) { this.lastAuthorizedTime = lastAuthorizedTime; }
        public Date getLastConsumedTime() { return lastConsumedTime; }
        public void setLastConsumedTime(Date lastConsumedTime) { this.lastConsumedTime = lastConsumedTime; }
        public Integer getStatus() { return status; }
        public void setStatus(Integer status) { this.status = status; }
        public Date getCreateTime() { return createTime; }
        public void setCreateTime(Date createTime) { this.createTime = createTime; }
        public String getFailureReason() { return failureReason; }
        public void setFailureReason(String failureReason) { this.failureReason = failureReason; }
    }
    
    /**
     * 授权统计信息DTO
     */
    class AuthorizationStats {
        private int totalTemplates;      // 总模板数
        private int totalAuthorizations; // 总授权次数
        private int totalConsumed;       // 总消费次数
        private int totalRemaining;      // 剩余授权次数
        private Date queryTime;          // 查询时间
        
        // getters and setters
        public int getTotalTemplates() { return totalTemplates; }
        public void setTotalTemplates(int totalTemplates) { this.totalTemplates = totalTemplates; }
        public int getTotalAuthorizations() { return totalAuthorizations; }
        public void setTotalAuthorizations(int totalAuthorizations) { this.totalAuthorizations = totalAuthorizations; }
        public int getTotalConsumed() { return totalConsumed; }
        public void setTotalConsumed(int totalConsumed) { this.totalConsumed = totalConsumed; }
        public int getTotalRemaining() { return totalRemaining; }
        public void setTotalRemaining(int totalRemaining) { this.totalRemaining = totalRemaining; }
        public Date getQueryTime() { return queryTime; }
        public void setQueryTime(Date queryTime) { this.queryTime = queryTime; }
    }
}
