using bdAntifraude.db;
using bdAntifraude.dbcontext;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using Org.BouncyCastle.Asn1.Ocsp;
using Serialize.Linq.Serializers;
using System.Linq.Expressions;

namespace SwaggerAntifraude.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class FAMILIAController : GenericoController<FAMILIA, int>
    {
        public FAMILIAController()
           : base()
        {
        }

        [Authorize(Policy = "LecturaPolicy")]
        [HttpPost("ParentescoExtend")]
        public async Task<IActionResult> ParentescoExtend( ExpressionWrapper request)
        {
            try
            {
                using (var context = tsGestionAntifraude.NuevoContexto(SoloLectura: true, UseLazyLoadingProxies: false))
                {
                    // Crear el deserializador
                    var serializer = new ExpressionSerializer(new Serialize.Linq.Serializers.JsonSerializer());

                    // Deserializar la expresión
                    var deserializedExpression = serializer.DeserializeText(request.Expression) as Expression<Func<FAMILIA, bool>>;
                    if (deserializedExpression == null)
                    {
                        return BadRequest("La expresión deserializada es nula o incorrecta.");
                    }

                    var motAdmin = await context.FAMILIA
                        .Where(deserializedExpression)
                        .Include(v => v.IDPARENTESCONavigation)
                        .AsNoTracking().ToListAsync();
                    if (motAdmin == null)
                        return NotFound();

                    return Ok(motAdmin);
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"Error interno del servidor: {ex.Message}");
            }
        }


    }
}
