package cn.reghao.tnb.content.app.vod.service;

import cn.reghao.jutil.jdk.web.db.Page;
import cn.reghao.jutil.jdk.web.db.PageList;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.app.vod.db.mapper.PostTagMapper;
import cn.reghao.tnb.content.app.vod.db.mapper.VideoTagMapper;
import cn.reghao.tnb.content.app.vod.model.po.PostTag;
import cn.reghao.tnb.content.app.vod.model.po.VideoTag;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author reghao
 * @date 2025-10-20 11:04:05
 */
@Service
public class TagService {
    private final int pageSize = 12;
    private final PostTagMapper postTagMapper;
    private final VideoTagMapper videoTagMapper;
    private final ContentPermission contentPermission;

    public TagService(PostTagMapper postTagMapper, VideoTagMapper videoTagMapper,
                      ContentPermission contentPermission) {
        this.postTagMapper = postTagMapper;
        this.videoTagMapper = videoTagMapper;
        this.contentPermission = contentPermission;
    }

    @Transactional(rollbackFor = Exception.class)
    public void addVideoTags(String videoId, int scope, String tags) {
        if (tags.isBlank()) {
            return;
        }

        List<PostTag> list = new ArrayList<>();
        List<VideoTag> list1 = new ArrayList<>();
        for (String str : tags.split(",")) {
            String tag = StringUtils.trimAllWhitespace(str);
            PostTag postTag = postTagMapper.findByName(tag);
            if (postTag != null) {
                list1.add(new VideoTag(videoId, scope, postTag.getTagId()));
            } else {
                String tagId = UUID.randomUUID().toString().replace("-", "");
                list.add(new PostTag(tagId, tag));
                list1.add(new VideoTag(videoId, scope, tagId));
            }
        }

        if (!list.isEmpty()) {
            postTagMapper.saveAll(list);
        }

        if (!list1.isEmpty()) {
            videoTagMapper.saveAll(list1);
        }
    }

    public List<String> getVideoTagNames(String videoId) {
        List<String> tagNames = videoTagMapper.findVideoTags(videoId);
        return tagNames;
    }

    public PageList<String> getVideoIdsByTagName(String tagName, int pageNumber) {
        long loginUser = UserContext.getUserId();
        List<Integer> userScopes = contentPermission.getUserScopes(loginUser);

        PostTag postTag = postTagMapper.findByName(tagName);
        if (postTag == null) {
            return PageList.empty();
        }
        String tagId = postTag.getTagId();

        int total = videoTagMapper.countVideosByTag(tagId, userScopes);
        Page page = new Page(pageNumber, pageSize);
        List<String> videoIds = videoTagMapper.findVideosByPage(page, tagId, userScopes);
        return PageList.pageList(pageNumber, pageSize, total, videoIds);
    }
}
