using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.Queries;
using PARR.API.Contracts.V1.Responses;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
using PARR.Core.Services.RobotTask.Interfaces;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
using PARR.Domain.Exceptions;

namespace PARR.API.Controllers.V1
{
    [Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
    public class RobotTaskController : BaseApiController
    {
        private readonly IRobotTaskService _robotTaskService;
        private readonly CommonSettings _commonSettings;
        private readonly IMapper _mapper;
        private readonly IClientService _clientService;

        public RobotTaskController(
            IMapper mapper,
            IClientService clientService,
            IRobotTaskService robotTaskService,
            CommonSettings commonSettings
            )
        {
            _robotTaskService = robotTaskService;
            _commonSettings = commonSettings;
            _mapper = mapper;
            _clientService = clientService;
        }


        /// <summary>
        /// Получить задание для робота по коду робота и по статусу задания
        /// </summary>
        /// <param name="robotCode"></param>
        /// <param name="taskStatusCode"></param>
        /// <param name="requestQuery"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.RobotTask.GetByRobotAndStatusTask)]
        public async Task<IActionResult> GetByRobotAndStatusTask([FromRoute] RobotsEnum robotCode, [FromRoute] TaskStatusEnum taskStatusCode, [FromQuery] RobotTaskQuery requestQuery)
        {
            switch (robotCode)
            {
                case RobotsEnum.TemplateOrder:
                    var templateTask = await _robotTaskService.GetTemplateTaskAsync(
                                                                    taskStatusCode,
                                                                    requestQuery.SetInProgressStatus ?? false,
                                                                    _clientService.GetClientIp()?.ToString(),
                                                                    requestQuery.RobotId
                                                                );
                    var templateResponse = _mapper.Map<RobotTaskTemplateResponse>(templateTask);

                    return Ok(new Response<RobotTaskTemplateResponse>(templateResponse, true));
                case RobotsEnum.ScheduleOrder:
                    var historyIniciator = new HistoryInitiator
                    {
                        InitiatorComment = "Задание роботу, расписание.",
                        InitiatorIp = _clientService.GetClientIp()?.ToString(),
                        InitiatorParrComponentId = ParrComponentsEnum.Api
                    };
                    var scheduleTask = await _robotTaskService.GetScheduleTaskAsync(
                                                                    taskStatusCode,
                                                                    requestQuery.SetInProgressStatus ?? false,
                                                                    historyIniciator.InitiatorIp,
                                                                    requestQuery.RobotId,
                                                                    historyIniciator,
                                                                    _commonSettings.ScheduleCooldownDuration
                                                                );

                    var scheduleResponse = _mapper.Map<RobotTaskScheduleResponse>(scheduleTask);

                    return Ok(new Response<RobotTaskScheduleResponse>(scheduleResponse, true));
                default:
                    throw new AppValidationException("Некорректное значение robotCode");
            }
        }
    }
}
