package cn.teammodel.repository;

import cn.teammodel.model.entity.ai.ChatSession;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author winter
 * @create 2023-11-28 17:39
 */
@Repository
public interface ChatSessionRepository extends CosmosRepository<ChatSession, String> {
    ChatSession findChatSessionByIdAndCode(String id, String code);
    @Query("select c.id, c.code, c.title, c.userId, c.createTime from c where c.code = 'ChatSession' and c.id = @sessionId")
    List<ChatSession> findBySessionId(String sessionId);

    @Query("select c.id, c.code, c.title, c.userId, c.createTime, c.updateTime from c where c.code = 'ChatSession' and c.userId = @userId")
    List<ChatSession> findByUserId(String userId);

    @Query("SELECT value ARRAY_SLICE(c.history, -3) FROM c where c.id = @sessionId and c.code = 'ChatSession'")
    List<ChatSession.Message> findLatestMessage(String sessionId);

    @Query("select c.id, c.code, c.title, c.userId, c.createTime from c where c.code = 'ChatSession' and c.id = @userId")
    List<ChatSession> findCommentsById(String userId);
}
