package com.erp.inventory.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 库存预警记录实体
 * 记录所有触发的库存预警
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = false)
@TableName("stock_warning_records")
public class StockWarningRecord {

    /**
     * 主键ID
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 预警规则ID
     */
    private Long ruleId;

    /**
     * 规则名称
     */
    private String ruleName;

    /**
     * 规则类型：low_stock、high_stock、out_of_stock
     */
    private String ruleType;

    /**
     * SKU编码
     */
    private String skuCode;

    /**
     * SKU名称
     */
    private String skuName;

    /**
     * 仓库ID
     */
    private Long warehouseId;

    /**
     * 仓库名称
     */
    private String warehouseName;

    /**
     * 当前库存数量
     */
    private Integer currentQuantity;

    /**
     * 可用库存数量
     */
    private Integer availableQuantity;

    /**
     * 预警阈值
     */
    private Integer warningThreshold;

    /**
     * 预警时的库存与阈值差值
     */
    private Integer deficit;

    /**
     * 预警等级：info(通知)、warning(警告)、critical(紧急)
     */
    private String level;

    /**
     * 预警消息内容
     */
    private String message;

    /**
     * 预警状态：pending(待处理)、notified(已通知)、acknowledged(已确认)、resolved(已解决)、ignored(已忽略)
     */
    private String status;

    /**
     * 通知方式：system、email、dingtalk
     */
    private String notificationMethod;

    /**
     * 通知是否成功
     */
    private Integer notificationSuccess;

    /**
     * 通知失败原因
     */
    private String notificationError;

    /**
     * 确认时间
     */
    private LocalDateTime acknowledgedAt;

    /**
     * 确认人ID
     */
    private Long acknowledgedBy;

    /**
     * 确认人名称
     */
    private String acknowledgedByName;

    /**
     * 解决时间
     */
    private LocalDateTime resolvedAt;

    /**
     * 解决人ID
     */
    private Long resolvedBy;

    /**
     * 解决备注
     */
    private String resolvedRemark;

    /**
     * 版本号（用于乐观锁）
     */
    private Integer version;

    /**
     * 创建时间
     */
    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    // ==================== 枚举和常量 ====================

    /**
     * 预警等级枚举
     */
    public enum Level {
        INFO("info", "通知"),
        WARNING("warning", "警告"),
        CRITICAL("critical", "紧急");

        private final String code;
        private final String label;

        Level(String code, String label) {
            this.code = code;
            this.label = label;
        }

        public String getCode() {
            return code;
        }

        public String getLabel() {
            return label;
        }

        public static Level fromCode(String code) {
            for (Level level : values()) {
                if (level.code.equals(code)) {
                    return level;
                }
            }
            return null;
        }
    }

    /**
     * 预警状态枚举
     */
    public enum Status {
        PENDING("pending", "待处理"),
        NOTIFIED("notified", "已通知"),
        ACKNOWLEDGED("acknowledged", "已确认"),
        RESOLVED("resolved", "已解决"),
        IGNORED("ignored", "已忽略");

        private final String code;
        private final String label;

        Status(String code, String label) {
            this.code = code;
            this.label = label;
        }

        public String getCode() {
            return code;
        }

        public String getLabel() {
            return label;
        }

        public static Status fromCode(String code) {
            for (Status status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return null;
        }
    }
    
    /**
     * 获取等级标签
     */
    public String getLevelLabel() {
        if (level == null) return "";
        Level l = Level.fromCode(level);
        return l != null ? l.getLabel() : "";
    }
    
    /**
     * 获取状态标签
     */
    public String getStatusLabel() {
        if (status == null) return "";
        Status s = Status.fromCode(status);
        return s != null ? s.getLabel() : "";
    }
    
    /**
     * 获取规则类型标签
     */
    public String getRuleTypeLabel() {
        if (ruleType == null) return "";
        try {
            StockWarningRule.RuleType rt = StockWarningRule.RuleType.fromCode(ruleType);
            return rt != null ? rt.getLabel() : "";
        } catch (Exception e) {
            return "";
        }
    }
}
