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 IConfiguration _configuration;
        public BaseController(IUnitOfWork unitOfWork,
           IWebHostEnvironment env)
        {
            _unitOfWork = unitOfWork;
            _env = env;
           // _configuration = configuration;
        }
       /* [NonAction]
        [AllowAnonymous]
        [HttpPost("{EMailTemplate}")]
        public string EMailTemplate(string relativePath, string emailTemplate)
        {
            try
            {
                string templateFolderPath = Path.Combine(_webHostEnvironment.ContentRootPath, relativePath);
                string templateFilePath = Path.Combine(templateFolderPath, emailTemplate);

                if (System.IO.File.Exists(templateFilePath))
                {
                    string body = System.IO.File.ReadAllText(templateFilePath);
                    return body;
                }
                else
                {
                    Console.WriteLine($"File not found: {templateFilePath}");
                    return "Template file not found";
                }
            }
            catch (Exception ex)
            {
                var errorMessage = ex.ToString() ?? ex.InnerException.ToString();
               *//* PostErrorMessage(errorMessage, "WebApi");*//*
                throw;
            }
        }*/
    /*    [NonAction]
        [AllowAnonymous]
        [HttpPost("{GetRelativePath}")]
        public string GetRelativePath(string relativePath)
        {
            try
            {
                string templateFolderPath = Path.Combine(_webHostEnvironment.ContentRootPath, relativePath);
                return templateFolderPath;
            }
            catch (Exception)
            {
                throw;
            }
        }*/
      /*  [NonAction]
        [HttpPost("{ErrMessage}")]
        public async Task PostErrorMessage(string errMessage, string appName)
        {
            var errorMessage = new ErrorMessage
            {
                CreatedDate = DateTime.Now,
                Message = errMessage,
                Application = appName,
                CreatedBy = appName

            };
            await ErrorMessageService.PostErrorMessage(errorMessage);
        }*/
        [NonAction]
        [HttpPost("{ErrorHandling}")]
        protected async Task<IActionResult> ExecuteWithErrorHandling<T>(
        Func<Task<T>> operation, string methodName, bool isPost)
        {
            try
            {
                var result = await operation();
                if (isPost)
                {
                    return Ok(new { success = true, messCode = 1, message = "Operation completed successfully", data = result });
                }
                return Ok(result);

            }
            catch (Exception ex)
            {
                var errorMessage = ex.InnerException?.ToString() ?? ex.Message.ToString();
             //   await PostErrorMessage(errorMessage, $"WebApi {methodName}");
                return BadRequest(new { success = false, messCode = 0, message = errorMessage });
            }
        }
        [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);
        }
    }
}
