using adas_core.Application.Repositories.Interfaces; using adas_core.Domain.Models.AppSettings; using adas_core.Domain.Models.MongoModels; using adas_core.Domain.Models.Responses; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using MongoDB.Bson; using MongoDB.Driver; namespace adas_core.Infrastructure.Repositories; /// /// Repository implementation for managing CardDetailsConfig entities in MongoDB. /// Provides CRUD operations for display detail configurations used in nurse and smart displays. /// public class DisplayDetailConfigRepository : MongoRepository, IDisplayDetailConfigRepository { private readonly ApiSettings _apiSettings; private readonly ILogger _logger; /// /// Initializes a new instance of the DisplayDetailConfigRepository. /// /// The MongoDB database instance. /// API settings containing collection names configuration. /// Logger for repository operations. public DisplayDetailConfigRepository( IMongoDatabase database, ApiSettings apiSettings, ILogger logger ) : base(database) { _apiSettings = apiSettings; _logger = logger; } /// /// Gets the name of the collection for display detail configurations. /// /// The collection name from API settings. public override string GetCollectionName() { return _apiSettings.DisplayDetailConfig; } /// /// Retrieves all display detail configurations from the database. /// /// A list of all CardDetailsConfig entities. public async Task> GetAll() { var result = await Collection.Find(Builders.Filter.Empty).ToListAsync(); return result; } /// /// Retrieves a display detail configuration by its ID. /// /// The ObjectId of the detail configuration to retrieve. /// The CardDetailsConfig if found; otherwise, null. /// Throws an exception if MongoDB query fails; returns null instead. public async Task GetById(ObjectId configId) { try { var result = await Collection.FindAsync(Builders.Filter.Eq(p => p.Id, configId)); return await result.FirstOrDefaultAsync(); } catch (Exception ex) { _logger.LogError("Error on config card display repository on GetById Exception: {ex}", ex.Message); return null; } } /// /// Inserts a new display detail configuration and returns the inserted document. /// /// The CardDetailsConfig to insert. /// The inserted CardDetailsConfig, or null if insertion fails. public async Task InsertOneAsyncAndReturn(CardDetailsConfig config) { try { await Collection.InsertOneAsync(config); return config; } catch (Exception ex) { _logger.LogError(ex.Message); return null; } } /// /// Updates an existing display detail configuration with new values. /// /// The CardDetailsConfig with updated values. /// An UpdateResponse containing the modified count and the updated document. /// Throws an exception if MongoDB update fails; returns UpdateResponse with null document. public async Task> UpdateOne(CardDetailsConfig? config) { try { if (config == null) return new UpdateResponse(0, null); var filter = Builders.Filter.Eq(c => c.Id, config.Id); var update = Builders.Update .Set(c => c.NurseRows, config.NurseRows) .Set(c => c.SmartSections, config.SmartSections) .Set(c => c.Header, config.Header); // Realizamos la actualización var result = await Collection.UpdateOneAsync(filter, update); // Buscamos el documento actual (ya actualizado o el existente si no hubo cambios) var updatedDoc = await Collection.Find(filter).FirstOrDefaultAsync(); // result.ModifiedCount será 1 si cambió algo, o 0 si los datos eran idénticos return new UpdateResponse(result.ModifiedCount, updatedDoc); } catch (Exception e) { _logger.LogError(e.Message); return new UpdateResponse(0, null); } } /// /// Deletes a display detail configuration by its ID. /// /// The ObjectId of the configuration to delete. /// The deleted CardDetailsConfig if found; otherwise, null. public async Task DeleteOne(ObjectId configId) { return await DeleteAsync(configId); } /// /// Updates the display configuration ID reference. This method is not implemented. /// /// The ObjectId of the display configuration. /// The new reference ID to set. /// Always throws NotImplementedException. /// This method is not implemented. public Task UpdateCardConfigId(ObjectId? displayConfigId, ObjectId? resultId) { throw new NotImplementedException(); } }