package com.izouma.nineth.service;

import com.izouma.nineth.domain.HeatInfo;
import com.izouma.nineth.domain.NewsLike;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.enums.HeatType;
import com.izouma.nineth.repo.HeatInfoRepo;
import com.izouma.nineth.repo.AuctionActivityRepo;
import com.izouma.nineth.repo.NewsLikeRepo;
import com.izouma.nineth.repo.NewsRepo;
import com.izouma.nineth.repo.ShowroomRepo;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class NewsLikeService {

    private NewsLikeRepo        newsLikeRepo;
    private NewsRepo            newsRepo;
    private ShowroomRepo        showroomRepo;
    private SysConfigService    sysConfigService;
    private HeatInfoRepo        heatInfoRepo;
    private AuctionActivityRepo auctionActivityRepo;

    public Page<NewsLike> all(PageQuery pageQuery) {
        return newsLikeRepo.findAll(JpaUtils.toSpecification(pageQuery, NewsLike.class), JpaUtils.toPageRequest(pageQuery));
    }

    public void like(Long userId, Long newsId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndNewsId(userId, newsId);
        if (!list.isEmpty()) return;
        newsLikeRepo.save(NewsLike.builder()
                .userId(userId)
                .newsId(newsId)
                .build());
        newsRepo.addLike(newsId, 1);
    }

    public void unlike(Long userId, Long newsId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndNewsId(userId, newsId);
        if (!list.isEmpty()) {
            newsLikeRepo.deleteAll(list);
            newsRepo.addLike(newsId, -list.size());
        }
    }

    public void likeRoom(Long userId, Long roomId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndShowroomId(userId, roomId);
        if (!list.isEmpty()) return;
        newsLikeRepo.save(NewsLike.builder()
                .userId(userId)
                .showroomId(roomId)
                .build());
        showroomRepo.addLike(roomId, 1);

        int weight = sysConfigService.getInt("heat_like_weight");
        List<HeatInfo> heats = heatInfoRepo.findByUserIdAndShowroomIdAndType(userId, roomId, HeatType.LIKE);
        if (!heats.isEmpty()) return;
        heatInfoRepo.save(HeatInfo.builder()
                .showroomId(roomId)
                .userId(userId)
                .type(HeatType.LIKE)
                .value(weight)
                .build());
        showroomRepo.addHeat(roomId, weight);
    }

    public void unlikeRoom(Long userId, Long roomId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndShowroomId(userId, roomId);
        if (!list.isEmpty()) {
            newsLikeRepo.deleteAll(list);
            showroomRepo.addLike(roomId, -list.size());
        }
    }

    public void likeAuction(Long userId, Long auctionId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndAuctionId(userId, auctionId);
        if (!list.isEmpty()) return;
        newsLikeRepo.save(NewsLike.builder()
                .userId(userId)
                .auctionId(auctionId)
                .build());
        auctionActivityRepo.addLike(auctionId, 1);
    }

    public void unlikeAuction(Long userId, Long auctionId) {
        List<NewsLike> list = newsLikeRepo.findByUserIdAndAuctionId(userId, auctionId);
        if (!list.isEmpty()) {
            newsLikeRepo.deleteAll(list);
            auctionActivityRepo.addLike(auctionId, -list.size());
        }
    }
}
