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
{
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class RobotController : BaseApiController
    {
        private readonly IMapper mapper;
        private readonly IRobotRepository robotService;

        public RobotController(
            IMapper mapper,
            IRobotRepository robotService
            )
        {
            this.mapper = mapper;
            this.robotService = robotService;
        }


        /// <summary>
        /// Получить список роботов
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Robot.GetAll)]
        public async Task<IActionResult> GetAll()
        {
            var robots = await robotService.Get().OrderBy(t => t.Code).ToListAsync();

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

            var response = mapper.Map<List<RobotResponse>>(robots);

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