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

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Периоды распределения РР
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class DistributionPeriodController : BaseApiController
    {
        private readonly IDistributionPeriodRepository distributionPeriodService;
        private readonly IMapper mapper;

        public DistributionPeriodController(
            IDistributionPeriodRepository distributionPeriodService,
            IMapper mapper
            )
        {
            this.distributionPeriodService = distributionPeriodService;
            this.mapper = mapper;
        }


        /// <summary>
        /// Список периодов распределения
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.DistributionPeriod.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var periods = await distributionPeriodService.Get()
                                    .OrderBy(t => t.Name)
                                    .ToListAsync();

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

            var response = mapper.Map<List<DistributionPeriodResponse>>(periods);

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