using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.Core.Services.Shortcodes;
using PARR.Domain.Common.Roles;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Переменные составляющие полей объектов платформы автоматизации регламентных работ
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class ShortcodeController : BaseApiController
    {
        private readonly ILogger<ShortcodeController> logger;
        private readonly IShortcodesService shortcodesService;
        private readonly IMapper mapper;

        public ShortcodeController(
            ILogger<ShortcodeController> logger,
            IShortcodesService shortcodesService,
            IMapper mapper
            )
        {
            this.logger = logger;
            this.shortcodesService = shortcodesService;
            this.mapper = mapper;
        }


        /// <summary>
        /// Получить список всех переменных составляющих
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Shortcode.GetAll)]
        public async Task<IActionResult> GetAll()
        {

            var shortcodes = await shortcodesService.GetAvailableShortcodesAsync();

            if (!shortcodes.Any())
                return NoContent();

            var response = mapper.Map<List<ShortcodeResponse>>(shortcodes).OrderBy(t => t.Shortcode).ToList();

            return Ok(new Response<List<ShortcodeResponse>>(response, true));
        }
    }
}
