using AsbCloudApp.Data;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudInfrastructure.Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AsbCloudApp.Services;
using Moq;
using Xunit;

namespace AsbCloudWebApi.Tests.ServicesTests;

public class EventServiceTest
{
    private readonly AsbCloudDbContext context;
    private readonly CacheDb cacheDb;
    private readonly Mock<ITelemetryService> telemetryService;

    public EventServiceTest()
    {
        context = TestHelpter.MakeTestContext();
        cacheDb = new CacheDb();
        telemetryService = new Mock<ITelemetryService>();
        telemetryService.Setup(s => s.GetOrCreateTelemetryIdByUid(It.IsAny<string>()))
            .Returns(1);
        context.TelemetryEvents.RemoveRange(context.TelemetryEvents); 
        context.SaveChanges();
    }

    [Fact]
    public async Task It_should_save_two_telemetry_events()
    {
        var service = new EventService(context, cacheDb, telemetryService.Object);

        var dtos = new List<EventDto>
        {
            new EventDto {Id = 1, IdCategory = 1, Message = "Test message 1"},
            new EventDto {Id = 2, IdCategory = 1, Message = "Test message 2"}
        };

        await service.UpsertAsync("uid", dtos);
        
        Assert.Equal(2, context.TelemetryEvents.Count());
    }
}