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.Core.Repositories.Interfaces.Unit;
using PARR.Domain.Common.Roles;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Список КИИ ЭК
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class KiiUnitController : BaseApiController
    {
        private readonly IMapper mapper;
        private readonly IUnitKiiUnitRepository unitKiiUnitService;

        public KiiUnitController(
            IMapper mapper,
            IUnitKiiUnitRepository unitKiiUnitService
            )
        {
            this.mapper = mapper;
            this.unitKiiUnitService = unitKiiUnitService;
        }


        /// <summary>
        /// Список КИИ ЭК
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.KiiUnit.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var query = unitKiiUnitService.Get()
                            .Include(t => t.Unit)
                            .Select(t => t.Unit)
                            .OrderBy(t => t.Name);

            var units = await query.ToListAsync();

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

            var response = mapper.Map<List<UnitBaseResponse>>(units);

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


    }
}
