using AutoMapper;
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;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.Core.Repositories.Interfaces;
using PARR.Domain.Common.Roles;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Контроллер по управлению ScheduleEsppId у шаблона
    /// </summary>
    [Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
    public class TemplateScheduleEspp : BaseApiController
    {
        private readonly ITemplateRepository templateService;
        private readonly IMapper mapper;
        //private readonly IValidator<TemplateScheduleEsppIdRequest> validator;
        private readonly IUriService uriService;

        public TemplateScheduleEspp(
            ITemplateRepository templateService,
            IMapper mapper,
            //IValidator<TemplateScheduleEsppIdRequest> validator,
            IUriService uriService
            )
        {
            this.templateService = templateService;
            this.mapper = mapper;
            //this.validator = validator;
            this.uriService = uriService;
        }

        /// <summary>
        /// Получить ИД расписания ЕСПП по ИД шаблона
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.TemplateScheduleEspp.Get)]
        public async Task<IActionResult> Get([FromRoute] Guid templateId)
        {
            var template = await templateService.GetAsync(templateId);

            if (template == null)
                return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));

            return Ok(new Response<TemplateScheduleEsppIdResponse>(mapper.Map<TemplateScheduleEsppIdResponse>(template), true));
        }


        /// <summary>
        /// Задать ИД расписания ЕСПП шаблону с ИД
        /// </summary>
        /// <param name="templateId"></param>
        /// <returns></returns>
        [HttpPost(ApiRoutes.TemplateScheduleEspp.Create)]
        public async Task<IActionResult> SetEsppId([FromRoute] Guid templateId, [FromBody] TemplateScheduleEsppIdRequest request)
        {
            //var resultValidate = await validator.ValidateAsync(request);
            //if (!resultValidate.IsValid)
            //    return BadRequest(new Response(resultValidate.Errors));

            var template = await templateService.GetAsync(templateId);

            if (template == null)
                return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Не найден шаблон с id: {templateId}" } }));

            template.ScheduleEsppId = request.ScheduleEsppId;

            if (!await templateService.CommitAsync())
                return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при сохранении шаблона." } }));

            var createdUri = uriService.GetUri(ApiRoutes.TemplateScheduleEspp.Get, ApiRoutes.TemplateScheduleEspp.templateId, templateId);

            return Created(createdUri, new Response<TemplateScheduleEsppIdResponse>(mapper.Map<TemplateScheduleEsppIdResponse>(template), true));
        }

    }
}
