using ControlCenter.Data;
using ControlCenter.Models;
using Microsoft.EntityFrameworkCore;
namespace ControlCenter.Repositories;
///
/// EF Core implementation of .
///
/// Queries the agents table via
/// using snake_case column mappings defined in the .
/// All write operations call SaveChangesAsync immediately.
///
public class AgentStateRepository : IAgentStateRepository
{
private readonly ControlCenterDbContext _context;
private readonly ILogger _logger;
public AgentStateRepository(
ControlCenterDbContext context,
ILogger logger)
{
_context = context;
_logger = logger;
}
///
public async Task> GetAllAsync()
{
_logger.LogDebug("Fetching all agent states");
return await _context.AgentStates.ToListAsync();
}
///
public async Task GetBySessionKeyAsync(string sessionKey)
{
_logger.LogDebug("Looking up agent state by session key: {SessionKey}", sessionKey);
return await _context.AgentStates
.FirstOrDefaultAsync(a => a.SessionKey == sessionKey);
}
///
public async Task UpdateStatusAsync(Guid id, string status)
{
_logger.LogDebug("Updating agent status: {Id} → {Status}", id, status);
var entity = await _context.AgentStates.FindAsync(id);
if (entity is null)
{
_logger.LogWarning("Agent state not found: {Id}", id);
return null;
}
entity.Status = status;
entity.LastActivity = DateTime.UtcNow;
_context.AgentStates.Update(entity);
await _context.SaveChangesAsync();
return entity;
}
}