using audit_logs.Models.AppSettings;
using audit.Model;
using audit.Repositories;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using audit_logs.Repositories.Interfaces;
using MongoDB.Driver;
using System;
using audit_logs.Models;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace audit_logs.Repositories;



public class AuditRecordRepository : MongoRepository<AuditRecord>, IAuditRecordRepository
{
    public AuditRecordRepository(IMongoDatabase database) : base(database)
    {
    }

    public AuditRecordRepository(IOptions<MongoDbSettingsAudit> dbSettings) : base(dbSettings)
    {
    }
  
    public override string GetCollectionName()
    {
        return "audit_records";
    }

  

    public async Task DeleteBeforeDate(DateTime date)
    {
        var filter = Builders<AuditRecord>.Filter.Lt(ar => ar.ActionTime, date);
        //await Collection.DeleteManyAsync(filter);
    }

    public async Task<long> InsertBatch(IEnumerable<AuditRecord> auditRecords)
    {
        try
        {
            await Collection.InsertManyAsync(auditRecords);
            return auditRecords.Count();
        }
        catch
        {
            return 0;
        }
    }

    public async Task<List<AuditRecord>> FindAllFromOrigin(ObjectId originId)
    {
        var filter = Builders<AuditRecord>.Filter.Eq(ar => ar.RecordId, originId.ToString());
        return await Collection.Find(filter).ToListAsync();
    }
    
    // Método para buscar un registro de auditoría por su ID
    public async Task<AuditRecord> FindRecordByIdAsync(ObjectId id)
    {
        var filter = Builders<AuditRecord>.Filter.Eq(ar => ar.Id, id);
        return await Collection.Find(filter).FirstOrDefaultAsync();
    }
    public async Task<List<AuditRecord>> GetAllAuditRecordsAsync()
    {
        // Retrieve all documents from the AuditRecords collection
        return await Collection.Find(new BsonDocument()).ToListAsync();
    }

   
    public async Task<List<AuditRecord>> GetPaginatedAsync(int pageNumber, int pageSize)
    {
      //  var skip = (pageNumber - 1) * pageSize;
       // return await Collection.Find(new BsonDocument()).Sort(sort).Skip(skip).Limit(pageSize).ToListAsync();
        throw new NotImplementedException();
    }

    public Task<long> CountDocumentsAsync()
    {
        throw new NotImplementedException();
    }
}