using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fuel.Core
{
    public static class HttpRequestReader
    {
        private static IHttpContextAccessor? _httpContextAccessor;

        // 使用依赖注入配置静态属性
        public static void Configure(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        public static Guid GetCurrentBuId()
        {
            if (_httpContextAccessor == null || _httpContextAccessor.HttpContext == null)
            {
                return Guid.Empty;
            }
            var httpContext = _httpContextAccessor.HttpContext;
            var headerValue = httpContext.Request.Headers["CurrentBuId"].ToString();

            if (Guid.TryParse(headerValue, out Guid buId))
            {
                return buId;
            }
            return Guid.Empty;
        }
        /// <summary>
        /// 获取请求头值
        /// </summary>
        /// <param name="headerName"></param>
        /// <returns></returns>
        public static string? GetHeaderValue(string headerName)
        {
            if (_httpContextAccessor == null || _httpContextAccessor.HttpContext == null)
            {
                throw new InvalidOperationException("HttpContext is not available.");
            }

            if (string.IsNullOrWhiteSpace(headerName))
            {
                throw new ArgumentException("Header name cannot be null or empty.", nameof(headerName));
            }

            var httpContext = _httpContextAccessor.HttpContext;
            if (httpContext.Request.Headers.TryGetValue(headerName, out var headerValue))
            {
                return headerValue.ToString();
            }

            return null;
        }
    }
}
