package com.kospo.talk.repository;

import com.kospo.talk.model.TalkMember;
import com.kospo.talk.model.TalkMemberId;
import com.kospo.talk.payload.ITalkMemberDto;
import com.kospo.talk.payload.TalkMemberDto;
import jakarta.transaction.Transactional;
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 java.util.List;

public interface TalkMemberRepository extends JpaRepository<TalkMember, TalkMemberId> {
    List<TalkMember> findByIdTalkId(String talkId);

    @Query(value="select tm.*, u.online_yn from talk_member tm inner join users u\n" +
            "on tm.sabun = u.sabun and tm.talk_id = :talkId and dummy_yn = false", nativeQuery = true)
    List<ITalkMemberDto> findByTalkId(@Param("talkId") String talkId);

    @Query(value="select tm.*, u.online_yn from talk_member tm inner join users u\n" +
            "on tm.sabun = u.sabun and tm.sabun = :sabun and dummy_yn = false", nativeQuery = true)
    List<ITalkMemberDto> findBySabun(@Param("sabun") String sabun);


    @Query(value="WITH TargetTalks AS (\n" +
            "    SELECT talk_id\n" +
            "    FROM talk_member\n" +
            "    WHERE sabun = :sabun\n" +
            "    and dummy_yn = false\n" +
            ")\n" +
            "SELECT tm.sabun\n" +
            "FROM talk_member tm\n" +
            "WHERE tm.talk_id IN (SELECT talk_id FROM TargetTalks)\n" +
            "  AND tm.dummy_yn = FALSE\n" +
            "  AND tm.delete_yn = FALSE\n" +
            "  AND EXISTS (SELECT 1 FROM talk t WHERE t.talk_id = tm.talk_id AND t.close_yn = FALSE)\n" +
            "GROUP BY tm.sabun", nativeQuery = true)
    List<String> findBySabunGroupBySabun(@Param("sabun") String sabun);

    @Query(value="select talk_member.talk_id, talk_member.sabun, cast(max(cast(users.online_yn as integer)) as boolean) as online_yn\n" +
            "from (select talk_member.* from talk inner join talk_member on talk.talk_id = talk_member.talk_id) talk_member\n" +
            "         inner join users on talk_member.sabun = users.sabun\n" +
            " and talk_member.talk_id in (\n" +
            "        select talk_id from talk_member where sabun = :sabun\n" +
            "        )\n" +
            "and dummy_yn = false\n" +
            "group by talk_member.talk_id, talk_member.sabun order by talk_member.talk_id\n", nativeQuery = true)
    List<ITalkMemberDto> findTalkOnlineYnInSabun(@Param("sabun") String sabun);

    @Transactional
    @Modifying
    @Query(value="update talk_member set delete_yn = true where talk_id = :talkId", nativeQuery = true)
    int disableTalkId(@Param("talkId") String talkId);

    @Transactional
    @Modifying
    @Query(value="update talk_member set delete_yn = false where talk_id = :talkId and sabun = :sabun", nativeQuery = true)
    int enableTalkIdAndSabun(@Param("talkId") String talkId, @Param("sabun") String sabun);

    @Transactional
    @Modifying
    @Query(value="insert into talk_member\n" +
            "select :talkId, sabun, name, mark_yn, delete_yn, true from talk_member  where talk_id = :pTalkId", nativeQuery = true)
    int dataCopy(@Param("pTalkId") String pTalkId, @Param("talkId") String talkId);

    @Transactional
    @Modifying
    @Query(value="update talk_member\n" +
            "set dummy_yn = false  where talk_id = :talkId and sabun = :sabun", nativeQuery = true)
    int dummyUpdate(@Param("talkId") String talkId, @Param("sabun") String sabun);


    @Query(value = "select * from talk_member where talk_id = :talkId and sabun != 'info' and dummy_yn = false", nativeQuery = true)
    List<TalkMember> findDeleteTarget(@Param("talkId") String talkId);
}