using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using survey_beta.DTOs.Create;
using survey_beta.DTOs.Default;

[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly UsersServices _usersServices;

    public UserController(UsersServices usersServices)
    {
        _usersServices = usersServices;
    }

    [HttpPost("register")]
    public async Task<ActionResult<UserDto>> Register([FromBody] CreateUserDto createUserDto)
    {
        try
        {
            var user = await _usersServices.CreateUserAsync(createUserDto);
            return Ok(user);
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }
    }

    [HttpPost("login")]
    public async Task<ActionResult<UserDto>> Login([FromBody] LoginDto loginDto)
    {
        try
        {
            var user = await _usersServices.SignInAsync(loginDto);
            return Ok(user);
        }
        catch (Exception ex)
        {
            return Unauthorized(ex.Message);
        }
    }
    //[Authorize]
    [HttpGet("{id}")]
    public async Task<IActionResult> GetUserById(string id)
    {
        try
        {
            var user = await _usersServices.GetUserByIdAsync(id);
            return Ok(user);
        }
        catch (Exception ex)
        {
            return NotFound(new { message = ex.Message });
        }
    }
    //[Authorize]
    [HttpGet("by-username/{username}")]
    public async Task<IActionResult> GetUserByUsername(string username)
    {
        try
        {
            var user = await _usersServices.GetUserByUsernameAsync(username);
            return Ok(user);
        }
        catch (Exception ex)
        {
            return NotFound(new { message = ex.Message });
        }
    }
    //[Authorize]
    [HttpGet("All-Users")]
    public async Task<IActionResult> GetAllUsers()
    {
        var users = await _usersServices.GetAllUsersAsync();
        return Ok(users);
    }
    [HttpDelete("Delete-User")]
    public async Task<IActionResult> DeleteUser(string id)
    {
        var users = await _usersServices.GetUserByIdAsync(id);
        if (users == null)
            return NotFound();
        var result = await _usersServices.DeleteUsersAsync(id);
        return Ok(result);
    }
        

}
