package com.example.demo.model;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import com.example.demo.model.enums.CommonTaskType;

/**
 * 积分冻结记录实体
 * 记录每次积分冻结的详细信息
 */
@Entity
@Table(name = "points_freeze_records")
public class PointsFreezeRecord {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "username", nullable = false, length = 100)
    private String username;

    @Column(name = "task_id", nullable = false, length = 50)
    private String taskId;

    @Enumerated(EnumType.STRING)
    @Column(name = "task_type", nullable = false, length = 20)
    private CommonTaskType taskType;

    @Column(name = "freeze_points", nullable = false)
    private Integer freezePoints; // 冻结的积分数量

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private FreezeStatus status; // 冻结状态

    @Column(name = "freeze_reason", length = 200)
    private String freezeReason; // 冻结原因

    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    @Column(name = "completed_at")
    private LocalDateTime completedAt;

    /**
     * 任务类型枚举
     */
    // TaskType 枚举已统一到 com.example.demo.model.enums.CommonTaskType

    /**
     * 冻结状态枚举
     */
    public enum FreezeStatus {
        FROZEN("已冻结"),
        DEDUCTED("已扣除"),
        RETURNED("已返还"),
        EXPIRED("已过期");

        private final String description;

        FreezeStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }

    // 构造函数
    public PointsFreezeRecord() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    public PointsFreezeRecord(String username, String taskId, CommonTaskType taskType, Integer freezePoints, String freezeReason) {
        this();
        this.username = username;
        this.taskId = taskId;
        this.taskType = taskType;
        this.freezePoints = freezePoints;
        this.freezeReason = freezeReason;
        this.status = FreezeStatus.FROZEN;
    }

    /**
     * 更新状态
     */
    public void updateStatus(FreezeStatus newStatus) {
        this.status = newStatus;
        this.updatedAt = LocalDateTime.now();
        
        if (newStatus == FreezeStatus.DEDUCTED || 
            newStatus == FreezeStatus.RETURNED || 
            newStatus == FreezeStatus.EXPIRED) {
            this.completedAt = LocalDateTime.now();
        }
    }

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public CommonTaskType getTaskType() {
        return taskType;
    }

    public void setTaskType(CommonTaskType taskType) {
        this.taskType = taskType;
    }

    public Integer getFreezePoints() {
        return freezePoints;
    }

    public void setFreezePoints(Integer freezePoints) {
        this.freezePoints = freezePoints;
    }

    public FreezeStatus getStatus() {
        return status;
    }

    public void setStatus(FreezeStatus status) {
        this.status = status;
    }

    public String getFreezeReason() {
        return freezeReason;
    }

    public void setFreezeReason(String freezeReason) {
        this.freezeReason = freezeReason;
    }

    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 LocalDateTime getCompletedAt() {
        return completedAt;
    }

    public void setCompletedAt(LocalDateTime completedAt) {
        this.completedAt = completedAt;
    }
}















