using Entities.Interfaces; using Entities.Models; using Microsoft.EntityFrameworkCore; namespace Infrastructure.Repository { public class RobotRepository : GenericRepository, IRobotRepository { private readonly MyDbContext _context; public RobotRepository(MyDbContext context) : base(context) { _context = context; } public async Task GetByIdAsync(int robotId) { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .FirstOrDefaultAsync(r => r.Id == robotId); } public async Task> GetAllAsync() { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .OrderBy(r => r.Name) .ToListAsync(); } public async Task> GetByStatusAsync(RobotStatus status) { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .Where(r => r.Status == status) .OrderBy(r => r.Name) .ToListAsync(); } public async Task> GetByTypeAsync(RobotType type) { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .Where(r => r.Type == type) .OrderBy(r => r.Name) .ToListAsync(); } public async Task> GetByTypeAndStatusAsync(RobotType type, RobotStatus status) { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .Where(r => r.Type == type && r.Status == status) .OrderByDescending(r => r.BatteryLevel) .ToListAsync(); } public async Task> GetAvailableRobotsAsync() { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.ActiveOrders) .Where(r => (r.Status == RobotStatus.Idle && r.BatteryLevel >= 20) || (r.Status == RobotStatus.Charging && r.BatteryLevel >= 20)) .OrderByDescending(r => r.BatteryLevel) .ToListAsync(); } public async Task UpdateAsync(Robot robot) { _context.Robots.Update(robot); await _context.SaveChangesAsync(); } public async Task DeleteAsync(int robotId) { var robot = await _context.Robots.FindAsync(robotId); if (robot != null) { _context.Robots.Remove(robot); await _context.SaveChangesAsync(); } } public async Task ExistsAsync(int robotId) { return await _context.Robots.AnyAsync(r => r.Id == robotId); } public async Task GetBySerialNumberAsync(string serialNumber) { return await _context.Robots .Include(r => r.CurrentNode) .Include(r => r.TargetNode) .Include(r => r.ActiveOrders) .FirstOrDefaultAsync(r => r.SerialNumber == serialNumber); } public async Task SerialNumberExistsAsync(string serialNumber) { return await _context.Robots.AnyAsync(r => r.SerialNumber == serialNumber); } } }