using DAL.Repositories;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using StaffDBServer.Controllers;
using Xunit;
using XUnitWebApi.SharedConfig;
using XUnitWebApi.Test;

namespace XUnitWebApi.Controller
{
    public class Test_Controller_Entity : TestBuilder
    {
        //----Check_GetEntityController

        [Fact]
        public async Task Check_GetSubsidiaryController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<SubsidiaryController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.SubsidiaryId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamInputFolderController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 49;
                var controller = Provider.GetService<WindreamInputFolderController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamInputFolderId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamSearchToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamSearchToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamSearchToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamSearchItemToWindreamSearchToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamSearchItemToWindreamSearchToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamSearchItemToWindreamSearchToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamSearchItemController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamSearchItemController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamSearchItemId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamSearchController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamSearchController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamSearchId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamIndexToWindreamSearchToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamIndexToWindreamSearchToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamIndexToWindreamSearchToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamIndexController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamIndexController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamIndexId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWindreamColumnsToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WindreamColumnsToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WindreamColumnsToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppToWebAppRoleController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 2;
                var controller = Provider.GetService<WebAppToWebAppRoleController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppToWebAppRoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppToWebAppAdditionalRoleController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 70;
                var controller = Provider.GetService<WebAppToWebAppAdditionalRoleController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppToWebAppAdditionalRoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 100;
                var controller = Provider.GetService<WebAppToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppAdditionalRoleController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WebAppAdditionalRoleController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppAdditionalRoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeToWebAppController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 300;
                var controller = Provider.GetService<EmployeeToWebAppController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeToWebAppId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<EmployeeToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeToAttributeController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 100;
                var controller = Provider.GetService<EmployeeToAttributeController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeToAttributeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<EmployeeController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetDocumentArtToDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<DocumentArtToDepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.DocumentArtToDepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetDepartmentToWebAppToEmployeeForWindreamController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<DepartmentToWebAppToEmployeeForWindreamController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.DepartmentToWebAppToEmployeeForWindreamId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppRoleController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 10;
                var controller = Provider.GetService<WebAppRoleController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppRoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetWebAppController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<WebAppController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.WebAppId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetRangController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<RangController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.RangId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetProjectController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<ProjectController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.ProjectId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeStatusController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<EmployeeStatusController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeStatusId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetEmployeeAttributeController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<EmployeeAttributeController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.EmployeeAttributeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetDocumentArtController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<DocumentArtController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.DocumentArtId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetDepartmentController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<DepartmentController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.DepartmentId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        [Fact]
        public async Task Check_GetCostCentreController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 100;
                var controller = Provider.GetService<CostCentreController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.CostCentreId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        [Fact]
        public async Task Check_Employee()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 272;
                var repo = Provider.GetService<EmployeeRepository>();
                var entity= await repo.GetByIdAsync(entityId);
                entity.PhoneNo = "1234";
                await repo.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    [Fact]
        public async Task Check_GetAdWebAppToWebAppRoleController()
        {
            Shared_Test_Config.Init_Webapi_Context();

            try
            {
                int entityId = 1;
                var controller = Provider.GetService<AdWebAppToWebAppRoleController>();
                dynamic result = await controller.GetEntityAsync(entityId);
                Assert.NotNull(result);
                Assert.Equal(StatusCodes.Status200OK, result.Result.StatusCode);

                var entity = result.Result.Value;
                Assert.NotNull(entity);
                if (entityId > 0) Assert.Equal(entityId, entity.AdWebAppToWebAppRoleId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}