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 = "user_works")
public class UserWork {

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

    @Column(name = "user_id", nullable = false)
    private Long userId;

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

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

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

    @Column(name = "title", length = 200)
    private String title; // 作品标题

    @Column(name = "description", columnDefinition = "TEXT")
    private String description; // 作品描述

    @Column(name = "prompt", columnDefinition = "TEXT")
    private String prompt; // 生成提示词

    @Column(name = "image_prompt", columnDefinition = "TEXT")
    private String imagePrompt; // 优化后的分镜图提示词

    @Column(name = "video_prompt", columnDefinition = "TEXT")
    private String videoPrompt; // 优化后的视频提示词

    @Column(name = "result_url", columnDefinition = "LONGTEXT")
    private String resultUrl; // 结果视频URL

    @Column(name = "thumbnail_url", columnDefinition = "LONGTEXT")
    private String thumbnailUrl; // 缩略图URL

    @Column(name = "duration", length = 10)
    private String duration; // 视频时长

    @Column(name = "aspect_ratio", length = 10)
    private String aspectRatio; // 宽高比

    @Column(name = "quality", length = 20)
    private String quality; // 画质 (HD/SD)

    @Column(name = "file_size", length = 20)
    private String fileSize; // 文件大小

    @Column(name = "points_cost", nullable = false)
    private Integer pointsCost; // 消耗积分

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private WorkStatus status; // 作品状态

    @Column(name = "is_public", nullable = false)
    private Boolean isPublic = false; // 是否公开

    @Column(name = "view_count", nullable = false)
    private Integer viewCount = 0; // 浏览次数

    @Column(name = "like_count", nullable = false)
    private Integer likeCount = 0; // 点赞次数

    @Column(name = "download_count", nullable = false)
    private Integer downloadCount = 0; // 下载次数

    @Column(name = "tags", length = 500)
    private String tags; // 标签，用逗号分隔

    @Column(name = "uploaded_images", columnDefinition = "LONGTEXT")
    private String uploadedImages; // 用户上传的参考图片（JSON数组），用于"做同款"功能恢复（分镜图阶段）

    @Column(name = "video_reference_images", columnDefinition = "LONGTEXT")
    private String videoReferenceImages; // 视频阶段用户上传的参考图片（JSON数组），用于分镜视频"做同款"功能恢复

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

    /**
     * 作品类型枚举
     */
    // WorkType 枚举已统一到 com.example.demo.model.enums.CommonTaskType

    /**
     * 作品状态枚举
     */
    public enum WorkStatus {
        PENDING("排队中"),
        PROCESSING("处理中"),
        COMPLETED("已完成"),
        FAILED("失败"),
        DELETED("已删除");

        private final String description;

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

        public String getDescription() {
            return description;
        }
    }

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

    public UserWork(String username, String taskId, CommonTaskType workType, String prompt, String resultUrl) {
        this();
        this.username = username;
        this.taskId = taskId;
        this.workType = workType;
        this.prompt = prompt;
        this.resultUrl = resultUrl;
        this.status = WorkStatus.COMPLETED;
        this.completedAt = LocalDateTime.now();
    }

    /**
     * 更新状态
     */
    public void updateStatus(WorkStatus newStatus) {
        this.status = newStatus;
        this.updatedAt = LocalDateTime.now();
        
        if (newStatus == WorkStatus.COMPLETED) {
            this.completedAt = LocalDateTime.now();
        }
    }

    /**
     * 增加浏览次数
     */
    public void incrementViewCount() {
        this.viewCount++;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 增加点赞次数
     */
    public void incrementLikeCount() {
        this.likeCount++;
        this.updatedAt = LocalDateTime.now();
    }

    /**
     * 增加下载次数
     */
    public void incrementDownloadCount() {
        this.downloadCount++;
        this.updatedAt = LocalDateTime.now();
    }

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

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

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    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 getWorkType() {
        return workType;
    }

    public void setWorkType(CommonTaskType workType) {
        this.workType = workType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPrompt() {
        return prompt;
    }

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

    public String getImagePrompt() {
        return imagePrompt;
    }

    public void setImagePrompt(String imagePrompt) {
        this.imagePrompt = imagePrompt;
    }

    public String getVideoPrompt() {
        return videoPrompt;
    }

    public void setVideoPrompt(String videoPrompt) {
        this.videoPrompt = videoPrompt;
    }

    public String getResultUrl() {
        return resultUrl;
    }

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

    public String getThumbnailUrl() {
        return thumbnailUrl;
    }

    public void setThumbnailUrl(String thumbnailUrl) {
        this.thumbnailUrl = thumbnailUrl;
    }

    public String getDuration() {
        return duration;
    }

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

    public String getAspectRatio() {
        return aspectRatio;
    }

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

    public String getQuality() {
        return quality;
    }

    public void setQuality(String quality) {
        this.quality = quality;
    }

    public String getFileSize() {
        return fileSize;
    }

    public void setFileSize(String fileSize) {
        this.fileSize = fileSize;
    }

    public Integer getPointsCost() {
        return pointsCost;
    }

    public void setPointsCost(Integer pointsCost) {
        this.pointsCost = pointsCost;
    }

    public WorkStatus getStatus() {
        return status;
    }

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

    public Boolean getIsPublic() {
        return isPublic;
    }

    public void setIsPublic(Boolean isPublic) {
        this.isPublic = isPublic;
    }

    public Integer getViewCount() {
        return viewCount;
    }

    public void setViewCount(Integer viewCount) {
        this.viewCount = viewCount;
    }

    public Integer getLikeCount() {
        return likeCount;
    }

    public void setLikeCount(Integer likeCount) {
        this.likeCount = likeCount;
    }

    public Integer getDownloadCount() {
        return downloadCount;
    }

    public void setDownloadCount(Integer downloadCount) {
        this.downloadCount = downloadCount;
    }

    public String getTags() {
        return tags;
    }

    public void setTags(String tags) {
        this.tags = tags;
    }

    public String getUploadedImages() {
        return uploadedImages;
    }

    public void setUploadedImages(String uploadedImages) {
        this.uploadedImages = uploadedImages;
    }

    public String getVideoReferenceImages() {
        return videoReferenceImages;
    }

    public void setVideoReferenceImages(String videoReferenceImages) {
        this.videoReferenceImages = videoReferenceImages;
    }

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

