using AutoMapper;
using VentaUniformes.Application.Interfaces;
using VentaUniformes.Application.Interfaces.External;
using VentaUniformes.Application.Persistencia.user.commands.CreateUser;
using VentaUniformes.Application.Persistencia.user.Queries.GetUserByPassAndName;
using VentaUniformes.Domain.Entities.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VentaUniformes.Application.Sesion.Commands.CreateSesion
{
    public class CreateSesionCommand: ICreateSesionCommand
    {
        private readonly IGetTokenJwtService _jwtService;
        private IGetUserByPassAndNameQuery _userService;

        public CreateSesionCommand(IGetTokenJwtService jwtService, IGetUserByPassAndNameQuery userService)
        {
            _jwtService = jwtService;
            _userService = userService;
        }
        public async Task<CreateSesionModel> Execute(InCreateSesionModel data)
        {

            var entity = await _userService.Execute(data.Password, data.UserName);
            if (entity == null)
                return null;
            var res = new CreateSesionModel();
            res.UserId = entity.Id;
            res.Role = (int)entity.Role;

            res.Token =  _jwtService.Execute(res.UserId.ToString(), entity.Role);
            return res;
        }
    }
}
