using AsbCloudApp.Data;
using AsbCloudApp.Data.WellOperation;
using AsbCloudDb.Model;
using AsbCloudInfrastructure;
using AsbCloudWebApi.IntegrationTests.Clients;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Net;
using Xunit;

namespace AsbCloudWebApi.IntegrationTests.Controllers;

public class WellControllerTest : BaseIntegrationTest
{

    private static readonly WellOperationDto wellOperationDto = new()
    {
        DateStart = DateTimeOffset.UtcNow,
        Day = 1,
        DepthEnd = 1000,
        DepthStart = 500,
        DurationHours = 5,
        Id = 1,
        IdCategory = 5095,
        IdPlan = null,
        IdType = 1,
        IdUser = 1,
        IdWell = 1,
        IdWellSectionType = 1,
        NptHours = 5
    };

    private readonly IWellClient wellClient;
    private readonly IWellOperationClient wellOperationClient;

    public WellControllerTest(WebAppFactoryFixture factory)
        : base(factory)
    {
        wellClient = factory.GetAuthorizedHttpClient<IWellClient>(string.Empty);
        wellOperationClient = factory.GetAuthorizedHttpClient<IWellOperationClient>(string.Empty);
    }

    [Fact]
    public async Task CheckDateStartForWell_returns_success()
    {
        //act
        var wellOperationDto1 = wellOperationDto.Adapt<WellOperationDto>();
        wellOperationDto1.DateStart = DateTimeOffset.UtcNow;
        wellOperationDto1.Id = 2;

        var wellOperations = new List<WellOperationDto>() { wellOperationDto, wellOperationDto1 };
        var insertedRedult = await wellOperationClient.InsertRangeAsync(1, false, wellOperations);

        var wellResponse = await wellClient.GetWellsAsync();

        //assert
        Assert.Equal(HttpStatusCode.OK, wellResponse.StatusCode);
        Assert.NotNull(wellResponse.Content);

        var expectedCount = await dbContext.Wells.CountAsync();
        Assert.Equal(expectedCount, wellResponse.Content.Count());

        var actualFirstStartDate = wellResponse.Content.ElementAt(0).StartDate!.Value.ToUniversalTime();
        var expectedFirstStartDate = wellOperations.MinByOrDefault(o => o.DateStart)!.DateStart.ToUniversalTime();
        Assert.Equal(expectedFirstStartDate.ToString(), actualFirstStartDate.ToString());
    }
}