using Health.Application.AI;

namespace Health.Infrastructure.AI;

public sealed class EfAiConversationRepository(AppDbContext db) : IAiConversationRepository
{
    private readonly AppDbContext _db = db;

    public Task<Conversation?> GetOwnedAsync(Guid userId, Guid conversationId, CancellationToken ct) =>
        _db.Conversations.FirstOrDefaultAsync(c => c.Id == conversationId && c.UserId == userId, ct);

    public Task<Conversation?> GetAsync(Guid conversationId, CancellationToken ct) =>
        _db.Conversations.FirstOrDefaultAsync(c => c.Id == conversationId, ct);

    public async Task<IReadOnlyList<Conversation>> ListAsync(Guid userId, CancellationToken ct) =>
        await _db.Conversations
            .Where(c => c.UserId == userId)
            .OrderByDescending(c => c.UpdatedAt)
            .ToListAsync(ct);

    public async Task<IReadOnlyList<ConversationMessage>> GetMessagesAsync(Guid conversationId, CancellationToken ct) =>
        await _db.ConversationMessages
            .Where(m => m.ConversationId == conversationId)
            .OrderBy(m => m.CreatedAt)
            .ToListAsync(ct);

    public async Task<IReadOnlyList<ConversationMessage>> GetRecentMessagesAsync(Guid conversationId, int limit, CancellationToken ct) =>
        await _db.ConversationMessages
            .Where(m => m.ConversationId == conversationId)
            .OrderByDescending(m => m.CreatedAt)
            .Take(limit)
            .ToListAsync(ct);

    public async Task AddConversationAsync(Conversation conversation, CancellationToken ct) =>
        await _db.Conversations.AddAsync(conversation, ct);

    public async Task AddMessageAsync(ConversationMessage message, CancellationToken ct) =>
        await _db.ConversationMessages.AddAsync(message, ct);

    public void Delete(Conversation conversation) =>
        _db.Conversations.Remove(conversation);

    public Task SaveChangesAsync(CancellationToken ct) =>
        _db.SaveChangesAsync(ct);
}
