package com.yaoyuan.jiscuss.repository;

import com.yaoyuan.jiscuss.entity.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Repository
public interface PostsRepository extends JpaRepository<Post, Integer> {

        void deleteByDiscussionId(Integer discussionId);

    /**
     * @param dId
     * @return
     */
    @Query("from Post where discussionId = :dId")
    List<Post> findOneBy(@Param("dId") Integer dId);

    /** Returns the current max floor number for a discussion (0 if no posts yet). */
    @Query("SELECT COALESCE(MAX(p.number), 0) FROM Post p WHERE p.discussionId = :dId")
    int findMaxNumberByDiscussionId(@Param("dId") Integer dId);

    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.likeCount = COALESCE(p.likeCount, 0) + :delta WHERE p.id = :id")
    void adjustLikeCount(@Param("id") Integer id, @Param("delta") int delta);

    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.dislikeCount = COALESCE(p.dislikeCount, 0) + :delta WHERE p.id = :id")
    void adjustDislikeCount(@Param("id") Integer id, @Param("delta") int delta);

    @Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
            "u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname  \n" +
            "from post p  \n" +
            "left join user u on p.user_id = u.id \n" +
            "left join user u2 on p.create_id = u2.id \n" +
            "where p.discussion_id = :dId order by p.create_time desc"
            , nativeQuery = true)
    List<Map<String, Object>> findPostCustomById(@Param("dId") Integer dId);


    @Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
            "u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname  \n" +
            "from post p  \n" +
            "left join user u on p.user_id = u.id \n" +
            "left join user u2 on p.create_id = u2.id \n" +
            "where p.discussion_id = :dId and p.parent_id is null order by p.create_time asc"
            , nativeQuery = true)
    List<Map<String, Object>> findAllByDIdAndparentIdNullOldest(@Param("dId") Integer dId);

    @Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
            "u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname  \n" +
            "from post p  \n" +
            "left join user u on p.user_id = u.id \n" +
            "left join user u2 on p.create_id = u2.id \n" +
            "where p.discussion_id = :dId and p.parent_id is null order by p.create_time desc"
            , nativeQuery = true)
    List<Map<String, Object>> findAllByDIdAndparentIdNullNewest(@Param("dId") Integer dId);

    @Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
            "u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname  \n" +
            "from post p  \n" +
            "left join user u on p.user_id = u.id \n" +
            "left join user u2 on p.create_id = u2.id \n" +
            "where p.discussion_id = :dId and p.parent_id is null order by p.like_count desc, p.create_time desc"
            , nativeQuery = true)
    List<Map<String, Object>> findAllByDIdAndparentIdNullHot(@Param("dId") Integer dId);

    @Query(value = "select p.*,u.avatar as user_avatar ,u.username as user_username ,u.realname as user_realname ," +
            "u2.avatar as create_avatar ,u2.username as create_username ,u2.realname as create_realname  \n" +
            "from post p  \n" +
            "left join user u on p.user_id = u.id \n" +
            "left join user u2 on p.create_id = u2.id \n" +
            "where p.discussion_id = :dId and p.parent_id is not null order by p.create_time desc"
            , nativeQuery = true)
    List<Map<String, Object>> findAllByDIdAndparentIdNotNull(@Param("dId") Integer dId);


//    @Query("from Post where parentId is null and discussionId = :dId")
//    List<Post> findAllByDIdAndparentIdNull(@Param("dId")Integer dId);
//
//    @Query("from Post where parentId is not null and discussionId = :dId")
//    List<Post> findAllByDIdAndparentIdNotNull(@Param("dId")Integer dId);

    /** Posts (replies) created by a given user, most recent first. */
    @Query("SELECT p FROM Post p WHERE p.createId = :userId ORDER BY p.createTime DESC")
    org.springframework.data.domain.Page<Post> findByCreateIdOrderByCreateTimeDesc(
            @Param("userId") Integer userId, org.springframework.data.domain.Pageable pageable);

    long countByCreateId(Integer createId);
}
