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;

/**
 * 图生视频任务模型
 */
@Entity
@Table(name = "image_to_video_tasks")
public class ImageToVideoTask {

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

    @Column(name = "task_id", unique = true, nullable = false)
    private String taskId;

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

    @Column(name = "first_frame_url", nullable = false)
    private String firstFrameUrl;

    @Column(name = "last_frame_url")
    private String lastFrameUrl;

    @Column(name = "prompt", columnDefinition = "TEXT")
    private String prompt;

    @Column(name = "aspect_ratio", nullable = false)
    private String aspectRatio;

    @Column(name = "duration", nullable = false)
    private Integer duration;

    @Column(name = "hd_mode", nullable = false)
    private Boolean hdMode = false;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private TaskStatus status = TaskStatus.PROCESSING;  // 默认生成中

    @Column(name = "progress")
    private Integer progress = 0;

    @Column(name = "result_url", columnDefinition = "TEXT")
    private String resultUrl;

    @Column(name = "real_task_id")
    private String realTaskId;

    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    @Column(name = "cost_points")
    private Integer costPoints = 0;

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

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

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

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

    public ImageToVideoTask(String taskId, String username, String firstFrameUrl, String prompt, 
                           String aspectRatio, Integer duration, Boolean hdMode) {
        this();
        this.taskId = taskId;
        this.username = username;
        this.firstFrameUrl = firstFrameUrl;
        this.prompt = prompt;
        this.aspectRatio = aspectRatio;
        this.duration = duration;
        this.hdMode = hdMode;
        
        // 计算消耗积分
        this.costPoints = calculateCost();
    }

    /**
     * 计算任务消耗积分
     */
    private Integer calculateCost() {
        // 安全处理duration可能为null的情况
        Integer safeDuration = duration;
        int actualDuration = (safeDuration == null || safeDuration <= 0) ? 5 : safeDuration; // 使用默认时长但不修改字段
        
        int baseCost = 10; // 基础消耗
        int durationCost = actualDuration * 2; // 时长消耗
        int hdCost = (hdMode != null && hdMode) ? 20 : 0; // 高清模式消耗
        
        return baseCost + durationCost + hdCost;
    }

    /**
     * 更新任务状态
     */
    public void updateStatus(TaskStatus newStatus) {
        this.status = newStatus;
        this.updatedAt = LocalDateTime.now();
        
        // 任务结束状态都应该设置完成时间
        if (newStatus == TaskStatus.COMPLETED || newStatus == TaskStatus.FAILED || newStatus == TaskStatus.CANCELLED) {
            this.completedAt = LocalDateTime.now();
        }
    }

    /**
     * 更新进度
     */
    public void updateProgress(Integer progress) {
        this.progress = Math.min(100, Math.max(0, progress));
        this.updatedAt = LocalDateTime.now();
    }

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

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

    public String getTaskId() {
        return taskId;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getFirstFrameUrl() {
        return firstFrameUrl;
    }

    public void setFirstFrameUrl(String firstFrameUrl) {
        this.firstFrameUrl = firstFrameUrl;
    }

    public String getLastFrameUrl() {
        return lastFrameUrl;
    }

    public void setLastFrameUrl(String lastFrameUrl) {
        this.lastFrameUrl = lastFrameUrl;
    }

    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }

    public String getAspectRatio() {
        return aspectRatio;
    }

    public void setAspectRatio(String aspectRatio) {
        this.aspectRatio = aspectRatio;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public Boolean getHdMode() {
        return hdMode;
    }

    public void setHdMode(Boolean hdMode) {
        this.hdMode = hdMode;
    }

    public TaskStatus getStatus() {
        return status;
    }

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

    public Integer getProgress() {
        return progress;
    }

    public void setProgress(Integer progress) {
        this.progress = progress;
    }

    public String getResultUrl() {
        return resultUrl;
    }

    public void setResultUrl(String resultUrl) {
        this.resultUrl = resultUrl;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Integer getCostPoints() {
        return costPoints;
    }

    public void setCostPoints(Integer costPoints) {
        this.costPoints = costPoints;
    }

    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;
    }

    public String getRealTaskId() {
        return realTaskId;
    }

    public void setRealTaskId(String realTaskId) {
        this.realTaskId = realTaskId;
    }

    /**
     * 任务状态枚举
     */
    public enum TaskStatus {
        PENDING("等待中"),
        PROCESSING("处理中"),
        COMPLETED("已完成"),
        FAILED("失败"),
        CANCELLED("已取消");

        private final String description;

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

        public String getDescription() {
            return description;
        }
    }
}
