package com.gameplatform.server.model.entity.cooldown;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.time.LocalDateTime;

/**
 * 设备冷却状态实体
 */
@TableName("machine_cooldown")
public class MachineCooldown {
    
    @TableId(type = IdType.AUTO)
    private Long id;
    
    /**
     * 设备ID
     */
    private String machineId;
    
    /**
     * 冷却开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime cooldownStartTime;
    
    /**
     * 冷却结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime cooldownEndTime;
    
    /**
     * 冷却原因
     */
    private String reason;
    
    /**
     * 关联的链接任务ID
     */
    private Long linkTaskId;
    
    /**
     * 冷却状态：ACTIVE-活跃, EXPIRED-已过期, MANUALLY_REMOVED-手动移除
     */
    private String status;
    
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    // 构造函数
    public MachineCooldown() {}
    
    public MachineCooldown(String machineId, LocalDateTime cooldownStartTime, 
                          LocalDateTime cooldownEndTime, String reason, Long linkTaskId) {
        this.machineId = machineId;
        this.cooldownStartTime = cooldownStartTime;
        this.cooldownEndTime = cooldownEndTime;
        this.reason = reason;
        this.linkTaskId = linkTaskId;
        this.status = "ACTIVE";
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }
    
    // Getter and Setter methods
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getMachineId() {
        return machineId;
    }
    
    public void setMachineId(String machineId) {
        this.machineId = machineId;
    }
    
    public LocalDateTime getCooldownStartTime() {
        return cooldownStartTime;
    }
    
    public void setCooldownStartTime(LocalDateTime cooldownStartTime) {
        this.cooldownStartTime = cooldownStartTime;
    }
    
    public LocalDateTime getCooldownEndTime() {
        return cooldownEndTime;
    }
    
    public void setCooldownEndTime(LocalDateTime cooldownEndTime) {
        this.cooldownEndTime = cooldownEndTime;
    }
    
    public String getReason() {
        return reason;
    }
    
    public void setReason(String reason) {
        this.reason = reason;
    }
    
    public Long getLinkTaskId() {
        return linkTaskId;
    }
    
    public void setLinkTaskId(Long linkTaskId) {
        this.linkTaskId = linkTaskId;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    /**
     * 检查冷却是否仍然有效
     */
    public boolean isActive() {
        return "ACTIVE".equals(status) && 
               cooldownEndTime != null && 
               LocalDateTime.now().isBefore(cooldownEndTime);
    }
    
    /**
     * 获取剩余冷却时间（分钟）
     */
    public long getRemainingMinutes() {
        if (!isActive()) {
            return 0;
        }
        return java.time.Duration.between(LocalDateTime.now(), cooldownEndTime).toMinutes();
    }
    
    @Override
    public String toString() {
        return "MachineCooldown{" +
                "id=" + id +
                ", machineId='" + machineId + '\'' +
                ", cooldownStartTime=" + cooldownStartTime +
                ", cooldownEndTime=" + cooldownEndTime +
                ", reason='" + reason + '\'' +
                ", linkTaskId=" + linkTaskId +
                ", status='" + status + '\'' +
                ", createdAt=" + createdAt +
                ", updatedAt=" + updatedAt +
                '}';
    }
}

