package com.lanyuanxiaoyao.leopard.core.entity;

import com.lanyuanxiaoyao.leopard.core.Constants;
import com.lanyuanxiaoyao.leopard.core.entity.base.SimpleEnum;
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.Lob;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * 任务
 *
 * @author lanyuanxiaoyao
 * @version 20250829
 */
@Setter
@Getter
@ToString(callSuper = true)
@FieldNameConstants
@Entity
@DynamicUpdate
@DynamicInsert
@EntityListeners(AuditingEntityListener.class)
@Table(name = Constants.DATABASE_PREFIX + "task")
public class Task extends SimpleEntity {
  @Column(nullable = false)
  @Comment("任务名称")
  private String name;
  @Lob
  @Comment("任务描述")
  private String description;
  @Lob
  @Basic(fetch = FetchType.LAZY)
  @ToString.Exclude
  @Comment("错误信息")
  private String error;
  @Lob
  @Basic(fetch = FetchType.LAZY)
  @ToString.Exclude
  @Comment("任务结果")
  private String result;
  @Column(nullable = false)
  @Enumerated(EnumType.STRING)
  @Comment("任务状态")
  private Status status = Status.RUNNING;
  @Column(nullable = false)
  @Comment("任务进度")
  private Double step = 0.0;
  @Comment("任务开始时间")
  private LocalDateTime launchedTime;
  @Comment("任务结束时间")
  private LocalDateTime finishedTime;

  @Getter
  @AllArgsConstructor
  public enum Status implements SimpleEnum {
    RUNNING("执行中"),
    SUCCESS("成功"),
    FAILURE("失败"),
    CANCELED("取消");

    private final String chineseName;
  }
}
