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

namespace AsbCloudWebApi.Tests.ServicesTests;

public class ClusterServiceTest
{
    private readonly AsbCloudDbContext context;
    private readonly Mock<IWellService> wellService;

    private readonly List<Deposit> deposits = new()
    {
        new Deposit { Id = 1, Caption = "Test deposit 1" },
        new Deposit { Id = 2, Caption = "Test deposit 2" },
        new Deposit { Id = 3, Caption = "Test deposit 3" },
        new Deposit { Id = 4, Caption = "Test deposit 4" }
    };

    private readonly List<Cluster> clusters = new()
    {
        new Cluster { Id = 1, Caption = "Test cluster 1", IdDeposit = 1, Timezone = new SimpleTimezone()},
        new Cluster { Id = 2, Caption = "Test cluster 2", IdDeposit = 1, Timezone = new SimpleTimezone() },
        new Cluster { Id = 3, Caption = "Test cluster 3", IdDeposit = 2, Timezone = new SimpleTimezone() },
        new Cluster { Id = 4, Caption = "Test cluster 4", IdDeposit = 2, Timezone = new SimpleTimezone() }
    };

    private readonly List<Well> wells = new()
    {
        new Well { Id = 1, Caption = "Test well 1", IdCluster = 1 },
        new Well { Id = 2, Caption = "Test well 2", IdCluster = 2 },
        new Well { Id = 3, Caption = "Test well 3", IdCluster = 1 },
        new Well { Id = 4, Caption = "Test well 4", IdCluster = 2 }
    };

    private readonly List<CompanyType> companiesTypes = new()
    {
        new CompanyType { Id = 1, Caption = "test company type"}
    };

    private readonly List<Company> companies = new()
    {
        new Company { Id = 1, Caption = "Test company 1", IdCompanyType = 1},
        new Company { Id = 2, Caption = "Test company 2", IdCompanyType = 1}
    };

    private readonly List<RelationCompanyWell> relations = new()
    {
        new RelationCompanyWell { IdCompany = 1, IdWell = 1 },
        new RelationCompanyWell { IdCompany = 1, IdWell = 2 },
        new RelationCompanyWell { IdCompany = 2, IdWell = 2 }
    };

    private readonly List<WellSectionType> wellSectionTypes = new()
    {
        new WellSectionType { Id = 1, Caption = "Test well section type 1" }
    };

    private readonly List<DrillParams> drillParams = new()
    {
        new DrillParams {Id = 1, IdWell = 1, IdWellSectionType = 1},
        new DrillParams {Id = 2, IdWell = 2, IdWellSectionType = 1}
    };

    public ClusterServiceTest()
    {
        context = TestHelpter.MakeRealTestContext();
        wellService = new Mock<IWellService>();
        context.Deposits.RemoveRange(context.Deposits);
        context.Clusters.RemoveRange(context.Clusters);
        context.Wells.RemoveRange(context.Wells);
        context.CompaniesTypes.RemoveRange(context.CompaniesTypes);
        context.Companies.RemoveRange(context.Companies);
        context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells);
        context.WellSectionTypes.RemoveRange(context.WellSectionTypes);
        context.DrillParams.RemoveRange(context.DrillParams);
        if (context.ChangeTracker.HasChanges())
            context.SaveChanges();
        context.Deposits.AddRange(deposits);
        context.Clusters.AddRange(clusters);
        context.Wells.AddRange(wells);
        context.CompaniesTypes.AddRange(companiesTypes);
        context.Companies.AddRange(companies);
        context.RelationCompaniesWells.AddRange(relations);
        context.WellSectionTypes.AddRange(wellSectionTypes);
        context.DrillParams.AddRange(drillParams);
        context.SaveChanges();
    }

    ~ClusterServiceTest()
    {
        context.Deposits.RemoveRange(context.Deposits);
        context.Clusters.RemoveRange(context.Clusters);
        context.Wells.RemoveRange(context.Wells);
        context.CompaniesTypes.RemoveRange(context.CompaniesTypes);
        context.Companies.RemoveRange(context.Companies);
        context.RelationCompaniesWells.RemoveRange(context.RelationCompaniesWells);
        context.SaveChanges();
    }

    [Fact]
    public async Task GetDepositsAsync_returns_one_deposit()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetDepositsAsync(1);

        Assert.Single(dtos);
    }

    [Fact]
    public async Task GetDepositsAsync_returns_one_deposit_with_two_clusters()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetDepositsAsync(1);

        Assert.Equal(2, dtos.FirstOrDefault()?.Clusters.Count());
    }
    [Fact]
    public async Task GetDrillParamsAsync_returns_depositDtos()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetDepositsDrillParamsAsync(1);

        Assert.True(dtos.Any());
    }

    [Fact]
    public async Task GetDrillParamsAsync_returns_one_deposit()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetDepositsDrillParamsAsync(1);

        Assert.Single(dtos);
    }

    [Fact]
    public async Task GetDrillParamsAsync_returns_one_deposit_with_two_clusters()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetDepositsDrillParamsAsync(1);

        Assert.Equal(2, dtos.FirstOrDefault()?.Clusters.Count());
    }

    [Fact]
    public async Task GetClustersAsync_returns_two_dtos()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetClustersAsync(1);

        Assert.Equal(2, dtos.Count());
    }

    [Fact]
    public async Task GetClustersAsync_with_deposit_returns_two_clusters()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetClustersAsync(1, 1);

        Assert.Equal(2, dtos.Count());
    }

    [Fact]
    public async Task GetWellsAsync_returns_one_well_by_cluster_and_company()
    {
        var service = new ClusterService(context, wellService.Object);
        var dtos = await service.GetWellsAsync(1, 1);

        Assert.Single(dtos);
    }


}