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

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

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


        [HttpGet(ApiRoutes.JobGroupType.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var query = jobGroupTypeService.Get().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));
        }
    }
}
