using Marco.Pms.Model.MongoDBModels.Utility;
using Microsoft.Extensions.Configuration;
using MongoDB.Driver;

namespace Marco.Pms.Helpers.CacheHelper
{
    public class ReportCache
    {
        private readonly IMongoCollection<ProjectReportEmailMongoDB> _projectReportCollection;
        public ReportCache(IConfiguration configuration)
        {
            var connectionString = configuration["MongoDB:ConnectionString"];
            var mongoUrl = new MongoUrl(connectionString);
            var client = new MongoClient(mongoUrl); // Your MongoDB connection string
            var mongoDB = client.GetDatabase(mongoUrl.DatabaseName);  // Your MongoDB Database name
            _projectReportCollection = mongoDB.GetCollection<ProjectReportEmailMongoDB>("ProjectReportMail");
        }

        /// <summary>
        /// Retrieves project report emails from the cache based on their sent status.
        /// </summary>
        /// <param name="isSent">True to get sent reports, false to get unsent reports.</param>
        /// <returns>A list of ProjectReportEmailMongoDB objects.</returns>
        public async Task<List<ProjectReportEmailMongoDB>> GetProjectReportMailFromCache(bool isSent)
        {
            var filter = Builders<ProjectReportEmailMongoDB>.Filter.Eq(p => p.IsSent, isSent);
            var reports = await _projectReportCollection.Find(filter).ToListAsync();
            return reports;
        }

        /// <summary>
        /// Adds a project report email to the cache.
        /// </summary>
        /// <param name="report">The ProjectReportEmailMongoDB object to add.</param>
        /// <returns>A Task representing the asynchronous operation.</returns>
        public async Task AddProjectReportMailToCache(ProjectReportEmailMongoDB report)
        {
            // Consider adding validation or logging here.
            await _projectReportCollection.InsertOneAsync(report);
        }
    }
}
