using AsbCloudApp.Data.ProcessMaps.Operations;
using AsbCloudDb.Model.ProcessMapPlan.Operations;
using DocumentFormat.OpenXml.Drawing.Charts;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Xunit;

namespace AsbCloudWebApi.IntegrationTests.Controllers.ProcessMapPlan.Operations;
public class ProcessMapPlanRotorControllerTest : ProcessMapPlanBaseControllerTest<ProcessMapPlanRotor, ProcessMapPlanRotorDto>
{
    private readonly ProcessMapPlanRotorDto dto = new ProcessMapPlanRotorDto()
    {
        IdWell = 1,
        DepthStart = 1,
        DepthEnd = 2,
        RopMax = 3,
        PressureMax = 4,
        DifferentialPressure = 5,
        DifferentialPressureMax = 6,
        WeightOnBit = 7,
        WeightOnBitMax = 8,
        TopDriveTorque = 9,
        TopDriveTorqueMax = 10,
        Rpm = 11,
        RpmMax = 12,
        FlowRate = 13,
        FlowRateMax = 14,
        Note = "15",
        Id = 0,
        IdWellSectionType = 1,
    };

    private readonly ProcessMapPlanRotor entity = new ProcessMapPlanRotor()
    {
        IdWell = 1,
        DepthEnd = 10,
        DepthStart = 2,
        DifferentialPressure = 3,
        DifferentialPressureMax = 4,
        FlowRate = 5,
        FlowRateMax = 6,
        Id = 0,
        IdWellSectionType = 1,
        Note = "1",
        PressureMax = 2,
        Rpm = 3,
        RpmMax = 4,
        RopMax = 5,
        TopDriveTorque = 6,
        TopDriveTorqueMax = 7,
        WeightOnBit = 8,
        WeightOnBitMax = 9,
        IdAuthor = 1,
        IdEditor = 1,
        Creation = DateTimeOffset.UtcNow
    };

    public ProcessMapPlanRotorControllerTest(WebAppFactoryFixture factory) : base(factory, "ProcessMapPlanRotor")
    {
    }


    protected override ProcessMapPlanRotor? GetByWellId()
    {
        var entity = dbContext
           .Set<ProcessMapPlanRotor>()
           .Where(p => p.WeightOnBit == dto.WeightOnBit)
           .Where(p => p.WeightOnBitMax == dto.WeightOnBitMax)
           .Where(p => p.Note == dto.Note)
           .FirstOrDefault(p => p.IdWell == dto.IdWell);

        return entity;
    }

    protected override ProcessMapPlanRotor GetByNote(
        DbSet<ProcessMapPlanRotor> dbSet,
        ProcessMapPlanRotorDto dto)
    {
        var entity = dbSet.First(p => p.Note == dto.Note);

        return entity;
    }

    protected override ProcessMapPlanRotorDto GetByNote(
    IEnumerable<ProcessMapPlanRotorDto> dtos,
    ProcessMapPlanRotorDto dto)
    {
        var entity = dtos.First(p => p.Note == dto.Note);

        return entity;
    }


    [Fact]
    public async Task InsertRange_returns_success()
    {
        await InsertRangeSuccess(dto);
    }


    [Fact]
    public async Task InsertRange_returns_BadRequest_for_IdWellSectionType()
    {
        //arrange
        var badDto = dto.Adapt<ProcessMapPlanRotorDto>();
        badDto.IdWellSectionType = int.MaxValue;

        await InsertRangeFailed(badDto);
    }

    [Fact]
    public async Task ClearAndInsertRange_returns_success()
    {
        await ClearAndInsertRange(entity, dto);
    }

    [Fact]
    public async Task UpdateOrInsertRange_returns_success()
    {
        var dtoUpdate = dto.Adapt<ProcessMapPlanRotorDto>();
        dtoUpdate.IdWell = 0;
        dtoUpdate.Note = "nebuchadnezzar";
        dtoUpdate.DifferentialPressureMax++;
        dtoUpdate.DifferentialPressure++;
        dtoUpdate.FlowRate++;
        dtoUpdate.FlowRateMax++;
        dtoUpdate.RopMax++;
        dtoUpdate.WeightOnBit++;
        dtoUpdate.WeightOnBitMax++;
        dtoUpdate.DepthStart++;
        dtoUpdate.DepthEnd++;
        dtoUpdate.Rpm++;
        dtoUpdate.RpmMax++;
        dtoUpdate.TopDriveTorque++;
        dtoUpdate.TopDriveTorqueMax++;

        var dtoInsert = dtoUpdate.Adapt<ProcessMapPlanRotorDto>();
        dtoInsert.Id = 0;
        dtoInsert.Note = "nebuchad";
        dtoInsert.DifferentialPressureMax++;
        dtoInsert.DifferentialPressure++;
        dtoInsert.FlowRate++;
        dtoInsert.FlowRateMax++;
        dtoInsert.RopMax++;
        dtoInsert.WeightOnBit++;
        dtoInsert.WeightOnBitMax++;
        dtoInsert.DepthStart++;
        dtoInsert.DepthEnd++;
        dtoInsert.Rpm++;
        dtoInsert.RpmMax++;
        dtoInsert.TopDriveTorque++;
        dtoInsert.TopDriveTorqueMax++;

        await UpdateOrInsertRange(entity, dtoUpdate, dtoInsert);
    }

    [Fact]
    public async Task DeleteRange_returns_success()
    {
        await DeleteRange(entity, dto);
    }

    [Fact]
    public async Task Clear_returns_success()
    {
        await Clear(entity, dto);
    }

    [Fact]
    public async Task GetDatesChange_returns_success()
    {
        await GetDatesChange(entity, dto);
    }

    [Fact]
    public async Task Get_actual_returns_success()
    {
        await Get(entity, dto);
    }

    [Fact]
    public async Task Get_at_moment_returns_success()
    {
        await GetAtMoment(entity, dto);
    }

    [Fact]
    public async Task Get_by_updated_from_returns_success()
    {
        await GetByUpdated(entity, dto);
    }

    [Fact]
    public async Task Get_updated_returns_success()
    {
        await GetUpdated(entity, dto);
    }

    [Fact]
    public async Task Parse_returns_success()
    {
        await Parse(dto.IdWell, "ProcessMapPlanRotorValid.xlsx", dto);
    }

    [Fact]
    public async Task Parse_returns_success_for_result_with_warnings()
    {
        await ParseWithWarnings(dto.IdWell, "ProcessMapPlanRotorInvalid.xlsx");
    }
}