using audit_logs.Repositories;
using audit.Model;

namespace audit_test.Repository;

public class AuditRecordRepositoryTests
{
    private AuditRecordRepository _repository = null!;
    
    [SetUp]
    public void Setup()
    {
        _repository = new AuditRecordRepository (IntegrationDb.Database);
    }
    
    [Test]
    public async Task InsertRecord_RecordIsInsertedAndCanBeRetrieved()
    {
        var record = new AuditRecord
        {
            EntityType = "User",
            RecordId = "1",
            UserId = "123",
            ActionType = "Create",
            ActionTime = DateTime.UtcNow,
            Changes = new List<AuditRecord.Change>
            {
                new AuditRecord.Change { Field = "Username", OldValue = "oldUser", NewValue = "newUser", ValueType = "String" }
            },
            Reason = "Ingreso de paciente"
        };

        await _repository.InsertOneAsync(record);

        var retrievedRecord = await _repository.FindRecordByIdAsync(record.Id);

        Assert.IsNotNull(retrievedRecord);
        Assert.AreEqual(record.ActionType, retrievedRecord.ActionType);
        //Assert.AreEqual(record.Details, retrievedRecord.Details);
    }
    [Test]
    public async Task GetAllAuditRecords_ReturnsAllRecords()
    {
        // Prepare data
        var records = new List<AuditRecord>
        {
            new AuditRecord { EntityType = "User", RecordId = "1", UserId = "123", ActionType = "Create", ActionTime = DateTime.UtcNow, Reason = "Test Reason 1" },
            new AuditRecord { EntityType = "Admin", RecordId = "2", UserId = "456", ActionType = "Delete", ActionTime = DateTime.UtcNow, Reason = "Test Reason 2" }
        };

        foreach (var rec in records)
        {
            await _repository.InsertOneAsync(rec);
        }

        // Test GetAllAuditRecordsAsync method
        var allRecords = await _repository.GetAllAuditRecordsAsync();

        // Assert that all records are retrieved
        Assert.IsNotNull(allRecords);
        Assert.AreEqual(2, allRecords.Count);  // This assumes the database is empty before test starts
        Assert.IsTrue(allRecords.Exists(r => r.RecordId == "1" && r.EntityType == "User"));
        Assert.IsTrue(allRecords.Exists(r => r.RecordId == "2" && r.EntityType == "Admin"));
    }
    
    [TearDown]
    public async Task Cleanup()
    {
        
        await IntegrationDb.Database.DropCollectionAsync("audit_records");
       
        
    }




}