using Microsoft.AspNetCore.Mvc;
using Silk_Road_Api.Extensions;
using Silk_Road_Api.Models;
using Silk_Road_Api.Services;
using Silk_Road_Api.ViewModels;

namespace Silk_Road_Api.Controllers
{
    [ApiController]
    [Route("user")]
    public class UserController : ControllerBase
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost("register-user", Name ="RegisterUser")]
        public async Task<IActionResult> RegisterUserAsync([FromBody] RegistrationViewModel user)
        {
            try
            {
                await _userService.RegisterUser(user);
                return Ok();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
        [HttpPost("login-user", Name = "LoginUser")]
        public IActionResult LoginUser([FromBody] LoginViewModel loginViewModel)
        {
            try
            {
                var user = _userService.LoginUser(loginViewModel.username, loginViewModel.password);
                HttpContext.Session.Set<User>("user", user);
                return Ok();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

    }
}
