package com.dora.dto;

import com.dora.entity.AiTask;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class AiTaskDto {
    @Schema(description = "任务编号")
    private String taskNo;

    @Schema(description = "模型名称")
    private String modelName;

    @Schema(description = "任务状态")
    private String status;

    @Schema(description = "任务类型（text_to_image/text_to_video/image_to_video等）")
    private String taskType;

    @Schema(description = "续作ID（如速创Sora2返回的PID，前端用于续作判断）")
    private String providerPid;

    @Schema(description = "进度百分比")
    private Integer progress;
    
    @Schema(description = "提示词 (部分，用于列表展示)")
    private String promptSnippet;

    @Schema(description = "参考图片URL（图生视频任务使用）")
    private String imageUrl;

    @Schema(description = "结果URL或缩略图URL")
    private String resultUrl;

    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    @Schema(description = "完成时间")
    private LocalDateTime completeTime;
    
    @Schema(description = "失败原因")
    private String errorMessage;

    @Schema(description = "是否已发布到广场")
    private Boolean isPublished;

    @Schema(description = "发布状态：未发布(not_published)/已发布(published)/审核中(pending)/已隐藏(hidden)")
    private String publishStatus;

    @Schema(description = "广场作品编号（如果已发布）")
    private String workNo;

    public static AiTaskDto fromEntity(AiTask entity) {
        return fromEntity(entity, null);
    }

    public static AiTaskDto fromEntity(AiTask entity, com.dora.entity.PlazaWork plazaWork) {
        if (entity == null) {
            return null;
        }

        // 确定发布状态
        Boolean isPublished = false;
        String publishStatus = "not_published";
        String workNo = null;

        if (plazaWork != null) {
            isPublished = true;
            workNo = plazaWork.getWorkNo();
            
            // 根据广场作品的状态和审核状态确定发布状态
            if ("published".equals(plazaWork.getStatus())) {
                if ("pending".equals(plazaWork.getAuditStatus())) {
                    publishStatus = "pending";
                } else if ("approved".equals(plazaWork.getAuditStatus())) {
                    publishStatus = "published";
                } else if ("rejected".equals(plazaWork.getAuditStatus())) {
                    publishStatus = "rejected";
                } else {
                    publishStatus = "published"; // 默认已发布
                }
            } else if ("hidden".equals(plazaWork.getStatus())) {
                publishStatus = "hidden";
            } else if ("draft".equals(plazaWork.getStatus())) {
                publishStatus = "draft";
            }
        }

        return AiTaskDto.builder()
                .taskNo(entity.getTaskNo())
                .modelName(entity.getModelName())
                .status(entity.getStatus())
                .taskType(entity.getTaskType())
                .providerPid(entity.getProviderPid())
                .progress(entity.getProgress())
                .promptSnippet(entity.getPrompt())
                .imageUrl(entity.getImageUrl())
                .resultUrl(entity.getResultUrl())
                .createTime(entity.getCreateTime())
                .completeTime(entity.getCompleteTime())
                .errorMessage(entity.getErrorMessage())
                .isPublished(isPublished)
                .publishStatus(publishStatus)
                .workNo(workNo)
                .build();
    }
}
