namespace audit_test.Repository;

using audit_logs.Models.DTO;
using audit.Model;
using audit_logs.Services;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;

[TestFixture]
[Category("Integration")]
public class AuditServiceFilterbyTextAndDateTests
{
    private AuditService _service;

    [SetUp]
    public void Setup()
    {
        // Crear la instancia del servicio utilizando la base de datos real
        _service = new AuditService(IntegrationDbReal.Database);
    }

    [Test]
    public async Task VerifyAllRecordsLoaded_ReturnsCorrectCount()
    {
        // Act
        var allRecords = await _service.AuditLogRepository.GetAllAuditRecordsAsync();

        // Assert
        Assert.That(allRecords.Count(), Is.EqualTo(3), "All records from the JSON file should be loaded.");
    }

    [Test]
    public async Task GetAuditLogsAsync_FilterByDateRange_ReturnsCorrectRecords()
    {
        // Arrange
        var startDate = DateTime.ParseExact("2024-11-21T23:29:40.267Z", "yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime();
        var endDate = DateTime.ParseExact("2024-11-22T08:48:51.194Z", "yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture).ToUniversalTime();

        var auditLogParametersDto = new AuditLogTextOrDateSearchDTO
        {
            pageNumber = 1,
            pageSize = 10,
            startDate = startDate,
            endDate = endDate
        };

        // Act
        var paginatedResult = await _service.GetAuditLogsMatchingTextOrDateAsync(auditLogParametersDto);
       
        // Assert
        Assert.That(paginatedResult.Records.Count, Is.EqualTo(0), "Should return only records within the specified date range.");
    }

    [Test]
    public async Task GetAuditLogsAsync_FilterByTextAndDateRange_ReturnsCorrectRecords()
    {
        // Arrange
        var startDate = DateTime.Parse("2024-11-21T23:29:40.267Z");
        var endDate = DateTime.Parse("2024-11-22T08:48:51.194Z");
        var auditLogParametersDto = new AuditLogTextOrDateSearchDTO
        {
            pageNumber = 1,
            pageSize = 10,
            startDate = startDate,
            endDate = endDate,
            searchText = "673fc258d26d3f5086c49598"
        };

        // Act
        var paginatedResult = await _service.GetAuditLogsMatchingTextOrDateAsync(auditLogParametersDto);

        // Assert
        Assert.That(paginatedResult.Records.Count, Is.EqualTo(0), "Should return only records matching the text and date range.");
    }
}
