using DigitalData.UserManager.Application.Services;
using DigitalData.UserManager.Application.Services.Options;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace DigitalData.UserManager.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class EncryptionController : ControllerBase
    {
        private readonly Encryptor _encryptor;

        public EncryptionController(Encryptor encryptor)
        {
            _encryptor = encryptor;
        }

        [HttpPost("encrypt")]
        public IActionResult Encrypt([FromQuery] string plainText, [FromBody] EncryptionParameters? options = null)
        {
            string cipherText = options is null 
                ? _encryptor.Encrypt(plainText) 
                : Encryptor.Encrypt(plainText, options.Key, options.IV);

            return Ok(cipherText);
        }

        [HttpPost("decrypt")]
        public IActionResult Decrypt([FromQuery] string cipherText, [FromBody] EncryptionParameters? options = null)
        {
            var plainText = options is null 
                ? _encryptor.Decrypt(cipherText)
                : Encryptor.Decrypt(cipherText, options.Key, options.IV);

            return Ok(plainText);
        }

        [HttpGet]
        public IActionResult Generate()
        {
            var param = Encryptor.GenerateParameters();
            return Ok(param);
        }
    }
}