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 = "novel_comic_tasks")
public class NovelComicTask {

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

    @Column(nullable = false, unique = true, length = 100)
    private String taskId; // Coze executeId

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

    @Column(length = 200)
    private String theme; // 主题（选填）

    @Column(columnDefinition = "TEXT")
    private String storyBackground; // 故事背景

    @Column(columnDefinition = "TEXT")
    private String storyScript; // 故事文案

    @Column(length = 500)
    private String musicUrl; // 背景音乐 COS URL

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

    @Column(columnDefinition = "LONGTEXT")
    private String result; // 生成结果

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

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

    public NovelComicTask() {
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
        this.status = TaskStatus.PROCESSING;
    }

    public NovelComicTask(String taskId, String username, String theme,
                          String storyBackground, String storyScript, String musicUrl) {
        this();
        this.taskId = taskId;
        this.username = username;
        this.theme = theme;
        this.storyBackground = storyBackground;
        this.storyScript = storyScript;
        this.musicUrl = musicUrl;
    }

    public void markCompleted(String result) {
        this.status = TaskStatus.COMPLETED;
        this.result = result;
        this.updatedAt = LocalDateTime.now();
        this.completedAt = LocalDateTime.now();
    }

    public void markFailed(String errorMessage) {
        this.status = TaskStatus.FAILED;
        this.errorMessage = errorMessage;
        this.updatedAt = LocalDateTime.now();
        this.completedAt = 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 getTheme() { return theme; }
    public void setTheme(String theme) { this.theme = theme; }
    public String getStoryBackground() { return storyBackground; }
    public void setStoryBackground(String storyBackground) { this.storyBackground = storyBackground; }
    public String getStoryScript() { return storyScript; }
    public void setStoryScript(String storyScript) { this.storyScript = storyScript; }
    public String getMusicUrl() { return musicUrl; }
    public void setMusicUrl(String musicUrl) { this.musicUrl = musicUrl; }
    public TaskStatus getStatus() { return status; }
    public void setStatus(TaskStatus status) { this.status = status; }
    public String getResult() { return result; }
    public void setResult(String result) { this.result = result; }
    public String getErrorMessage() { return errorMessage; }
    public void setErrorMessage(String errorMessage) { this.errorMessage = errorMessage; }
    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; }
}
