using Managing.Api.Authorization;
using Managing.Api.Models.Requests;
using Managing.Application.Abstractions.Services;
using Managing.Domain.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Managing.Api.Controllers;

/// <summary>
/// Provides authentication-related actions, including token creation for user authentication.
/// </summary>
[ApiController]
[Route("[controller]")]
[Produces("application/json")]
public class UserController : BaseController
{
    private IConfiguration _config;
    private readonly IJwtUtils _jwtUtils;

    /// <summary>
    /// Initializes a new instance of the <see cref="UserController"/> class.
    /// </summary>
    /// <param name="config">Configuration settings.</param>
    /// <param name="userService">Service for user-related operations.</param>
    /// <param name="jwtUtils">Utility for JWT token operations.</param>
    public UserController(IConfiguration config, IUserService userService, IJwtUtils jwtUtils)
        : base(userService)
    {
        _config = config;
        _jwtUtils = jwtUtils;
    }

    /// <summary>
    /// Creates a JWT token for a user based on the provided login credentials.
    /// </summary>
    /// <param name="login">The login request containing user credentials.</param>
    /// <returns>A JWT token if authentication is successful; otherwise, an Unauthorized result.</returns>
    [AllowAnonymous]
    [HttpPost]
    public async Task<ActionResult<string>> CreateToken([FromBody] LoginRequest login)
    {
        var user = await _userService.Authenticate(login.Name, login.Address, login.Message, login.Signature);

        if (user != null)
        {
            var tokenString = _jwtUtils.GenerateJwtToken(user, login.Address);
            return Ok(tokenString);
        }

        return Unauthorized();
    }  

    /// <summary>
    /// Gets the current user's information.
    /// </summary>
    /// <returns>The current user's information.</returns>
    [HttpGet]
    public async Task<ActionResult<User>> GetCurrentUser()
    {
        var user = await base.GetUser();
        return Ok(user);
    }

    /// <summary>
    /// Updates the agent name for the current user.
    /// </summary>
    /// <param name="agentName">The new agent name to set.</param>
    /// <returns>The updated user with the new agent name.</returns>
    [HttpPut("agent-name")]
    public async Task<ActionResult<User>> UpdateAgentName([FromBody] string agentName)
    {
        var user = await GetUser();
        var updatedUser = await _userService.UpdateAgentName(user, agentName);
        return Ok(updatedUser);
    }

    /// <summary>
    /// Updates the avatar URL for the current user.
    /// </summary>
    /// <param name="avatarUrl">The new avatar URL to set.</param>
    /// <returns>The updated user with the new avatar URL.</returns>
    [HttpPut("avatar")]
    public async Task<ActionResult<User>> UpdateAvatarUrl([FromBody] string avatarUrl)
    {
        var user = await GetUser();
        var updatedUser = await _userService.UpdateAvatarUrl(user, avatarUrl);
        return Ok(updatedUser);
    }

    /// <summary>
    /// Updates the Telegram channel for the current user.
    /// </summary>
    /// <param name="telegramChannel">The new Telegram channel to set.</param>
    /// <returns>The updated user with the new Telegram channel.</returns>
    [HttpPut("telegram-channel")]
    public async Task<ActionResult<User>> UpdateTelegramChannel([FromBody] string telegramChannel)
    {
        var user = await GetUser();
        var updatedUser = await _userService.UpdateTelegramChannel(user, telegramChannel);
        return Ok(updatedUser);
    }
}  
    