using AfRApay.Web.Backend;
using AfRApay.Web.Controllers.Schema;
using LinqToDB;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Filters;

namespace AfRApay.Web.Controllers;

[ApiController]
public class UserController : Controller {
	/// <summary>
	/// Renames the specified user.
	/// </summary>
	/// <param name="uid">The ID of the user to be renamed</param>
	/// <param name="newName">The new name of the user</param>
	/// <response code="200">Returns 200 if user was renamed successfully</response>
	/// <response code="400">Returns 400 if newName isn't unique</response>
	/// <response code="404">Returns 404 if no user matching the UID was found</response>
	[HttpPut]
	[Produces("application/json")]
	[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserResponse))]
	[ProducesResponseType(StatusCodes.Status400BadRequest, Type = typeof(ErrorResponse))]
	[ProducesResponseType(StatusCodes.Status404NotFound, Type = typeof(ErrorResponse))]
	//[SwaggerResponseExample(400, typeof(new )]
	[Route("/api/user/{uid:int}/name")]
	public async Task<IActionResult> Rename(int uid, [FromQuery] string newName) {
		var db = new Database.DbConn();
		if (db.Users.Any(p => p.Id == uid)) {
			var user = db.Users.First(p => p.Id == uid);
			user.Nickname = newName;
			await db.UpdateAsync(user);
			return new OkObjectResult(new UserResponse(user));
		}

		return NotFound(new ErrorResponse("Unknown user"));
	}
}
