using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Pos.Api.Database;

namespace Pos.Api
{
    public class BaseController : Controller
    {
        private readonly IConfiguration _config;
        public BaseController(IConfiguration config)
        {
            _config = config;
        }
        public override async Task OnActionExecutionAsync(ActionExecutingContext context,
            ActionExecutionDelegate next)
        {
            HttpRequest request = Request;
            string apiKey = request.Headers["ApiKey"].ToString();
            string? apiKeyRequest = _config["ApiKey"];
            if (apiKeyRequest == null)
            {
                throw new NullReferenceException("ApiKey header is missing");
            }

            if (!apiKeyRequest.Equals(apiKey))
                throw new ArgumentException("ApiKey is wrong");
        }
    }
}
