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.JobGroupRepositories;
using PARR.Domain.Common.Roles;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Список типов групп работ
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class JobGroupTypeController : BaseApiController
    {
        private readonly IJobGroupTypeRepository jobGroupTypeRepository;
        private readonly IMapper mapper;

        public JobGroupTypeController(
            IJobGroupTypeRepository jobGroupTypeRepository,
            IMapper mapper
            )
        {
            this.jobGroupTypeRepository = jobGroupTypeRepository;
            this.mapper = mapper;
        }


        /// <summary>
        /// Получить список типов групп работ
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.JobGroupType.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var query = jobGroupTypeRepository.Get()
                                .AsNoTracking()
                                .OrderBy(t => t.Description);

            var types = await query.ToListAsync();

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

            var response = mapper.Map<List<JobGroupTypeResponse>>(types);

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