using AsbCloudApp.Exceptions;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;

namespace AsbCloudWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UserSettingsController : ControllerBase
    {
        private readonly IUserSettingsRepository service;

        public UserSettingsController(IUserSettingsRepository service)
        {
            this.service = service;
        }

        [HttpGet("{key}")]
        [Permission]
        public virtual async Task<IActionResult> GetAsync(
            [StringLength(255, MinimumLength = 1, ErrorMessage = "The key value cannot less then 1 character and greater then 255. ")]
            string key, 
            CancellationToken token)
        {
            var userId = User.GetUserId();
            if (userId is null)
                return Forbid();

            var result = await service.GetOrDefaultAsync((int)userId, key, token).ConfigureAwait(false);
            return Ok(result);
        }

        [HttpPost("{key}")]
        [Permission]
        public virtual async Task<ActionResult<int>> InsertAsync(string key, [FromBody] object value, CancellationToken token)
        {
            var userId = User.GetUserId();
            if (userId is null)
                return Forbid();

            var result = await service.InsertAsync((int)userId, key, value, token).ConfigureAwait(false);
            if (result ==  IUserSettingsRepository.ErrorKeyIsUsed)
                return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(key), "is already used"));
            return Ok(result);
        }

        [HttpPut("{key}")]
        public virtual async Task<ActionResult<int>> UpdateAsync(string key, [FromBody] object value, CancellationToken token)
        {
            var userId = User.GetUserId();
            if (userId is null)
                return Forbid();

            var result = await service.UpdateAsync((int)userId, key, value, token).ConfigureAwait(false);
            if (result < 0)
                return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(key), "not found"));
            return Ok(result);
        }

        [HttpDelete("{key}")]
        public virtual async Task<ActionResult<int>> DeleteAsync(string key, CancellationToken token)
        {
            var userId = User.GetUserId();
            if (userId is null)
                return Forbid();

            var result = await service.DeleteAsync((int)userId, key, token).ConfigureAwait(false);
            if (result < 0)
                return BadRequest(ArgumentInvalidException.MakeValidationError(nameof(key), "not found"));
            return Ok(result);
        }
    }
}