using LSFE.Domain.Contracts;
using LSFE.Infrastructure.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace LSFE.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BaseController : ControllerBase
    {
        private readonly IWebHostEnvironment _env;
        private readonly IUnitOfWork _unitOfWork;
        public BaseController(IUnitOfWork unitOfWork,
           IWebHostEnvironment env)
        {
            _unitOfWork = unitOfWork;
            _env = env;
        }
   
        [NonAction]
        [HttpPost("{ErrorHandling}")]
        protected async Task<IActionResult> ExecuteWithErrorHandling<T>(
        Func<Task<T>> operation, string methodName, bool isPost)
        {
            var result = await operation();
            if (isPost)
            {
                return Ok(new { success = true, messCode = 1, message = "Operation completed successfully", data = result });
            }
            return Ok(result);
        }
        [NonAction]
        public async Task<IActionResult> LogAndReturnBadRequest(string message, string? userId)
        {
            await LogErrorAsync(message, userId);
            return BadRequest(message);
        }
        [NonAction]
        public async Task LogErrorAsync(string message, string? userId)
        {
            var error = new ErrorLogs
            {
                Message = message,
                CreatedBy = userId ?? "Unknown",
                Application = "Mob_2_API",
                CreatedDate = DateTime.UtcNow
            };
            await _unitOfWork.ErrorLogger.LogErrorAsync(error);
        }
    }
}
