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 = "text_to_video_tasks")
public class TextToVideoTask {

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

    @Column(nullable = false, unique = true, length = 50)
    private String taskId;

    @Column(nullable = false, length = 100)
    private String username; // 关联用户

    @Column(columnDefinition = "TEXT")
    private String prompt; // 文本描述

    @Column(nullable = false, length = 10)
    private String aspectRatio; // 16:9, 4:3, 1:1, 3:4, 9:16

    @Column(nullable = false)
    private int duration; // in seconds, e.g., 5, 10, 15, 30

    @Column(nullable = false)
    private boolean hdMode; // 是否高清模式

    @Enumerated(EnumType.STRING)
    @Column(nullable = false, length = 20)
    private TaskStatus status;

    @Column(nullable = false)
    private int progress; // 0-100

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

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

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

    @Column(nullable = false)
    private int costPoints; // 消耗积分

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

    @Column(nullable = false)
    private LocalDateTime updatedAt;

    @Column
    private LocalDateTime completedAt;

    public enum TaskStatus {
        PENDING, PROCESSING, COMPLETED, FAILED, CANCELLED
    }

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

    public TextToVideoTask(String username, String prompt, String aspectRatio, int duration, boolean hdMode) {
        this();
        this.username = username;
        this.prompt = prompt;
        this.aspectRatio = aspectRatio;
        this.duration = duration;
        this.hdMode = hdMode;
        
        // 计算消耗积分
        this.costPoints = calculateCost();
    }

    /**
     * 计算任务消耗积分
     */
    private Integer calculateCost() {
        int actualDuration = duration <= 0 ? 5 : duration; // 使用默认时长但不修改字段
        
        int baseCost = 15; // 文生视频基础消耗比图生视频高
        int durationCost = actualDuration * 3; // 时长消耗
        int hdCost = hdMode ? 25 : 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 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 int getDuration() { return duration; }
    public void setDuration(int duration) { this.duration = duration; }
    public boolean isHdMode() { return hdMode; }
    public void setHdMode(boolean hdMode) { this.hdMode = hdMode; }
    public TaskStatus getStatus() { return status; }
    public void setStatus(TaskStatus status) { this.status = status; }
    public int getProgress() { return progress; }
    public void setProgress(int 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 String getRealTaskId() { return realTaskId; }
    public void setRealTaskId(String realTaskId) { this.realTaskId = realTaskId; }
    public int getCostPoints() { return costPoints; }
    public void setCostPoints(int 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; }
}
