package com.pablotj.ia.chat.boot.persistence;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;


public interface ChatMessageJpaRepository extends JpaRepository<ChatMessageEntity, Long> {

    @Query(value = """
            SELECT *
            FROM (
                SELECT *, ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY id ASC) AS rn
                FROM chat_messages
            ) sub
            WHERE rn = 1
            ORDER BY id desc
            """, nativeQuery = true)
    List<ChatMessageEntity> findFirstMessagePerSession();

    List<ChatMessageEntity> findBySessionIdOrderByIdAsc(String chatId);

    void deleteBySessionId(String chatId);
}