using Microsoft.AspNetCore.Http;
using Xunit;
using XUnitWebApi.SharedConfig;
using XUnitWebApi.SharedUtils;

namespace XUnitWebApi.SharedController
{
    public class Shared_Test_Controller_Generic
    {
        [Theory()]
        [InlineData("")]
        [InlineData("AdWebAppToWebAppRole")]
        [InlineData("CostCentre")]
        [InlineData("Department")]
        [InlineData("DocumentArt")]
        [InlineData("EmployeeAttribute")]
        [InlineData("EmployeeStatus")]
        [InlineData("Project")]
        [InlineData("Rang")]
        [InlineData("WebApp")]
        [InlineData("WebAppRole")]
        [InlineData("DepartmentToWebAppToEmployeeForWindream")]
        [InlineData("DocumentArtToDepartment")]
        [InlineData("Employee")]
        [InlineData("EmployeeToAttribute")]
        [InlineData("EmployeeToDepartment")]
        [InlineData("EmployeeToWebApp")]
        [InlineData("WebAppAdditionalRole")]
        [InlineData("WebAppToDepartment")]
        [InlineData("WebAppToWebAppAdditionalRole")]
        [InlineData("WebAppToWebAppRole")]
        [InlineData("WindreamColumnsToDepartment")]
        [InlineData("WindreamIndex")]
        [InlineData("WindreamIndexToWindreamSearchToDepartment")]
        [InlineData("WindreamSearch")]
        [InlineData("WindreamSearchItem")]
        [InlineData("WindreamSearchItemToWindreamSearchToDepartment")]
        [InlineData("WindreamSearchToDepartment")]
        [InlineData("WindreamInputFolder")]
        [InlineData("Subsidiary")]
        public async Task Check_Dynamic_EntityControllers(string entityName = "", int entityId = 0)
        {
            Shared_Test_Config.Init_Webapi_Context();
            string actualEntity = "";
            List<string> ExceptionList = new List<string>();

            var typeList = TestHelper.GetAllRepositories().Select(t => t.Name.Replace("Repository", ""));

            foreach (var item in typeList)
            {
                try
                {
                    actualEntity = item;
                    if (entityName == "" || item == entityName)
                    {
                        dynamic repository = TestHelper.GetRepository_BasedOn_BaseRepository(item);
                        if (repository == null) continue;
                        dynamic controller = TestHelper.GetApiController_BasedOn_BaseController(item, repository);
                        if (controller == null) continue;
                        dynamic result;
                        if (entityId > 0)
                        {
                            result = await controller.GetEntityAsync(entityId);
                            result = result?.Result;
                        }
                        else result = await controller.GetAllAsync();
                        Assert.NotNull(result);
                        Assert.Equal(StatusCodes.Status200OK, result.StatusCode);

                        var entity = result.Value;
                        Assert.NotNull(entity);

                        if (entityId > 0) Assert.Equal(entityId, entity.GetEntityId());
                        else Assert.NotEqual(entity.Count, 0);
                    }
                }
                catch (Exception ex)
                {
                    //throw new Exception(actualEntity, ex);
                    Console.WriteLine(ex.Message);
                    ExceptionList.Add($"Entity: {actualEntity} " + ex.Message + " " + ex.InnerException);
                }
            }
            var errList = "";
            foreach (var item in ExceptionList) errList += "\r\n -- " + item;
            if (ExceptionList.Count > 0) throw new Exception(errList);
        }
    }
}