package com.yaoai.api.dto.ai;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yaoai.domain.entity.Storyboard;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Data
public class StoryboardDTO {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final TypeReference<List<String>> LIST_STRING_TYPE = new TypeReference<>() {};

    private Long id;
    private Long episodeId;
    private Integer sequenceNum;
    private String sceneNumber;
    private String shortDescription;
    private String detailedDescription;
    private String characters;
    private String dialogues;
    private String cameraDirection;
    private String compositionGuide;
    private Integer durationSeconds;
    private String startFramePrompt;
    private String motionScript;
    private String sceneRef;
    private String videoPrompt;
    private String notes;
    private String status;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    /** V16：分镜上落地的素材选择 */
    private List<String> characterImageKeys;
    private String sceneImageKey;
    private List<String> propImageKeys;
    private String styleImageKey;
    private String firstFrameImageKey;
    private String firstFrameImageUrl;
    private String lastFrameImageKey;
    private String lastFrameImageUrl;

    public static StoryboardDTO from(Storyboard s) {
        StoryboardDTO dto = new StoryboardDTO();
        dto.setId(s.getId());
        dto.setEpisodeId(s.getEpisodeId());
        dto.setSequenceNum(s.getSequenceNum());
        dto.setSceneNumber(s.getSceneNumber());
        dto.setShortDescription(s.getShortDescription());
        dto.setDetailedDescription(s.getDetailedDescription());
        dto.setCharacters(s.getCharacters());
        dto.setDialogues(s.getDialogues());
        dto.setCameraDirection(s.getCameraDirection());
        dto.setCompositionGuide(s.getCompositionGuide());
        dto.setDurationSeconds(s.getDurationSeconds());
        dto.setStartFramePrompt(s.getStartFramePrompt());
        dto.setMotionScript(s.getMotionScript());
        dto.setSceneRef(s.getSceneRef());
        dto.setVideoPrompt(s.getVideoPrompt());
        dto.setNotes(s.getNotes());
        dto.setStatus(s.getStatus());
        dto.setCreatedAt(s.getCreatedAt());
        dto.setUpdatedAt(s.getUpdatedAt());

        dto.setCharacterImageKeys(parseList(s.getCharacterImageKeys(), "characterImageKeys", s.getId()));
        dto.setSceneImageKey(s.getSceneImageKey());
        dto.setPropImageKeys(parseList(s.getPropImageKeys(), "propImageKeys", s.getId()));
        dto.setStyleImageKey(s.getStyleImageKey());
        dto.setFirstFrameImageKey(s.getFirstFrameImageKey());
        dto.setFirstFrameImageUrl(s.getFirstFrameImageUrl());
        dto.setLastFrameImageKey(s.getLastFrameImageKey());
        dto.setLastFrameImageUrl(s.getLastFrameImageUrl());
        return dto;
    }

    private static List<String> parseList(String json, String fieldName, Long id) {
        if (json == null || json.isBlank()) return null;
        try {
            return OBJECT_MAPPER.readValue(json, LIST_STRING_TYPE);
        } catch (Exception e) {
            log.warn("Failed to parse {} for storyboard {}: {}", fieldName, id, e.getMessage());
            return null;
        }
    }
}
