using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
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.Constants;
using PARR.DAL.Services.Interfaces;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Управление типами приложений
    /// </summary>

    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class ApplicationTypeController : BaseApiController
    {
        private readonly IMapper mapper;
        private readonly IApplicationTypeService applicationTypeService;

        public ApplicationTypeController(
             IMapper mapper,
             IApplicationTypeService applicationTypeService
            )
        {
            this.mapper = mapper;
            this.applicationTypeService = applicationTypeService;
        }


        /// <summary>
        /// Список типов приложений
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.ApplicationType.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var appTypes = await applicationTypeService.Get().OrderBy(t => t.Description)
                                    .ToListAsync();

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

            var response = mapper.Map<List<ApplicationTypeResponse>>(appTypes);

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

    }
}
