using audit.Model;
using audit_logs.Repositories.Interfaces;
using audit_logs.Services;

namespace audit_test.Service;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Xunit;

public class AuditServiceTests
{
    private readonly Mock<IAuditRecordRepository> _mockRepository;
        private readonly AuditService _auditService;
        private readonly AuditRecord _testAuditRecord;

        public AuditServiceTests()
        {
            // creo el mock del repositorio
            _mockRepository = new Mock<IAuditRecordRepository>();

            // preparo el servicio con el mock de repositorio
            _auditService = new AuditService(_mockRepository.Object);

            // configuracion del registro
            _testAuditRecord = 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",
                UserIpAddress = "127.0.0.1" 
            };
        }

        [Fact]
        public async Task RecordAuditAsync_ValidRecord_InsertsRecord()
        {
            // Arrange
            _mockRepository.Setup(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()))
                           .Returns(Task.CompletedTask);

            // Act
            await _auditService.RecordAuditAsync(_testAuditRecord);

            // Assert
            _mockRepository.Verify(repo => repo.InsertOneAsync(It.IsAny<AuditRecord>()), Times.Once);
        }

        [Fact]
        public async Task RecordAuditAsync_NullRecord_ThrowsArgumentNullException()
        {
            // Act & Assert
            await Assert.ThrowsAsync<ArgumentNullException>(() => 
                _auditService.RecordAuditAsync(null));
        }
}