using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests;
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.Common.Interfaces.RabbitServices;
using PARR.Core.Repositories.Interfaces.Job;
using PARR.Domain.Common.Rabbit.Messages;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Отправка запросов на активацию/деактивацию шаблонов/расписаний
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class TemplateActivatorController : BaseApiController
    {
        //private readonly IValidator<TemplateActivatorRequest> validator;
        private readonly IRabbitService mqService;
        private readonly IUriService uriService;
        private readonly MqSettings mqSettings;
        private readonly IJobRepository jobService;
        private readonly IClientService clientService;

        public TemplateActivatorController(
            //IValidator<TemplateActivatorRequest> validator,
            IRabbitService mqService,
            IUriService uriService,
            MqSettings mqSettings,
            IJobRepository jobService,
            IClientService clientService
            )
        {
            //this.validator = validator;
            this.mqService = mqService;
            this.uriService = uriService;
            this.mqSettings = mqSettings;
            this.jobService = jobService;
            this.clientService = clientService;
        }


        /// <summary>
        /// Отправить запрос на активацию/деактивацию шаблонов/расписаний
        /// </summary>
        /// <returns></returns>
        [HttpPost(ApiRoutes.TemplateActivator.SendRequest)]
        public async Task<IActionResult> SendRequest([FromBody] TemplateActivatorRequest request)
        {
            //var resultValidate = await validator.ValidateAsync(request);
            //if (!resultValidate.IsValid)
            //    return BadRequest(new Response(resultValidate.Errors));

            var mqRequest = new TemplateActivatorMq
            {
                Id = request.Id,
                EntityType = request.EntityType,
                EsppObject = request.EsppObject,
                NewState = request.NewState,
                HistoryInitiator = new HistoryInitiator { InitiatorIp = clientService.GetClientIp()?.ToString(), InitiatorParrComponentId = ParrComponentsEnum.Api }
            };

            //var msg = JsonSerializer.Serialize(mqRequest);

            //var sendResult = await mqService.SendAsync(mqSettings.TemplateActivator, new[] { msg });
            var sendResult = await mqService.SendAsync(mqSettings.TemplateActivator, new List<object> { mqRequest });

            if (!sendResult.IsSuccess)
                return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при отправке данных." } }));

            var createdUri = uriService.GetAllUri(ApiRoutes.Template.GetAll);

            return Created(createdUri, new Response<string?>(null, true, new List<ErrorModel>(), "Отправлен запрос активатору шаблонов/расписаний."));
        }
    }
}
