package com.starry.admin.modules.weichat.assembler;

import com.starry.admin.modules.clerk.module.entity.PlayClerkMediaAssetEntity;
import com.starry.admin.modules.media.entity.PlayMediaEntity;
import com.starry.admin.modules.weichat.entity.clerk.MediaVo;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public final class ClerkMediaAssembler {

    private ClerkMediaAssembler() {
    }

    public static MediaVo toVo(PlayMediaEntity media, PlayClerkMediaAssetEntity asset) {
        if (media == null || asset == null || Boolean.TRUE.equals(asset.getDeleted())) {
            return null;
        }
        MediaVo vo = new MediaVo();
        vo.setId(media.getId());
        vo.setMediaId(media.getId());
        vo.setAssetId(asset.getId());
        vo.setKind(media.getKind());
        vo.setStatus(media.getStatus());
        vo.setUrl(media.getUrl());
        vo.setCoverUrl(media.getCoverUrl());
        vo.setDurationMs(media.getDurationMs());
        vo.setWidth(media.getWidth());
        vo.setHeight(media.getHeight());
        vo.setSizeBytes(media.getSizeBytes());
        vo.setOrderIndex(asset.getOrderIndex());
        vo.setMetadata(media.getMetadata());
        vo.setUsage(asset.getUsage());
        vo.setReviewState(asset.getReviewState());
        vo.setSubmittedTime(asset.getSubmittedTime());
        vo.setReviewNote(asset.getNote());
        return vo;
    }

    public static List<MediaVo> toVoList(List<PlayClerkMediaAssetEntity> assets,
            Map<String, PlayMediaEntity> mediaById) {
        if (assets == null || assets.isEmpty()) {
            return Collections.emptyList();
        }
        return assets.stream()
                .map(asset -> toVo(mediaById.get(asset.getMediaId()), asset))
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}
