using VentaUniformes.Application.Interfaces.External;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using VentaUniformes.Domain.Enums;

namespace VentaUniformes.External.GetTokenJWT
{
    public class GetTokenJwtService: IGetTokenJwtService
    {
        private readonly IConfiguration _config;
        public GetTokenJwtService(IConfiguration config) 
        { 
            _config= config;
        }
        public string Execute(string id, RoleUserType role)
        {
            
            var tokenHandler = new JwtSecurityTokenHandler();
            string key = _config["SecretKeyJwt"]??"";
            var singinkey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject= new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.NameIdentifier, id),
                    new Claim(ClaimTypes.Role, role.ToString())
                }),
                Expires= DateTime.UtcNow.AddMinutes(int.Parse(_config["TimeLogin"])),//expira en un dia
                SigningCredentials=new SigningCredentials(singinkey,SecurityAlgorithms.HmacSha256Signature),
                Issuer = _config["IssuerJWT"],
                Audience = _config["AudienceJwt"],
                
            };
          
            var token= tokenHandler.CreateToken(tokenDescriptor);

            var tokenString = tokenHandler.WriteToken(token);
            return tokenString;           
        }
    }
}
