using System.Text.Json;
using bitforum.Constants;
using bitforum.Models;

namespace bitforum.Middleware
{
    // 환경변수 구조
    public class AppConfig
    {
        public string AppName { get; set; }
        public string AppVersion { get; set; }
    }

    public class Common
    {
        private readonly RequestDelegate _next;
        private readonly IConfiguration _configuration;

        public Common(RequestDelegate next, IConfiguration configuration)
        {
            _next = next;
            _configuration = configuration;
        }

        public async Task InvokeAsync(HttpContext context)
        {
         
            AppConfig? appConfig = null;

            try
            {
                appConfig = _configuration.GetSection("AppConfig").Get<AppConfig>(); ;
            }
            catch (JsonException ex)
            {
                Console.WriteLine($"JSON Parsing Error: {ex.Message}");
            }

            context.Items["layoutViewModel"] = new LayoutViewModel
            {
                // 환경 변수 가져오기
                AppConfig = appConfig,

                // 메뉴 데이터 가져오기
                Menus = MenuData.GetMenus()
            };

            await _next(context);
        }
    }
}
