using Marco.Pms.Model.TenantModels.MongoDBModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;

namespace Marco.Pms.Helpers.Utility
{
    public class FeatureDetailsHelper
    {
        private readonly IMongoCollection<FeatureDetails> _collection;
        private readonly ILogger<FeatureDetailsHelper> _logger;
        public FeatureDetailsHelper(IConfiguration configuration, ILogger<FeatureDetailsHelper> logger)
        {
            _logger = logger;
            var connectionString = configuration["MongoDB:ModificationConnectionString"];
            var mongoUrl = new MongoUrl(connectionString);
            var client = new MongoClient(mongoUrl); // Your MongoDB connection string
            var mongoDB = client.GetDatabase(mongoUrl.DatabaseName);  // Your MongoDB Database name
            _collection = mongoDB.GetCollection<FeatureDetails>("FeatureDetails");
        }
        public async Task<FeatureDetails?> GetFeatureDetails(Guid Id)
        {
            try
            {
                var filter = Builders<FeatureDetails>.Filter.Eq(e => e.Id, Id);

                var result = await _collection
                    .Find(filter)
                    .FirstOrDefaultAsync();
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception occured while fetchig features for subscription plan");
                return null;
            }
        }
        public async Task<bool> AddFeatureDetails(FeatureDetails featureDetails)
        {
            try
            {
                await _collection.InsertOneAsync(featureDetails);
                return true;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Exception occured while fetchig features for subscription plan");
                return false;
            }
        }

    }
}
