using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests;
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.Extensions;
using PARR.API.Services.Interfaces;
using PARR.Core.Common.Helpers;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.Schedule;
using PARR.Core.Services.Shortcodes;
using PARR.Domain.Common.Pagination;
using PARR.Domain.Common.Roles;
using PARR.Domain.Entities;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
using PARR.Domain.Settings;

namespace PARR.API.Controllers.V1
{
    /// <summary>
    /// Шаблоны
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class TemplateController : BaseApiController
    {
        private readonly IMapper _mapper;
        private readonly ITemplateRepository _templateRepository;
        private readonly IRobotConfigurationRepository _robotConfigurationRepository;
        private readonly IClientService _clientService;
        private readonly ILogger<TemplateController> _logger;
        private readonly IShortcodesService _shortcodesService;
        private readonly IOrderRepository _orderRepository;
        private readonly SettingsFromDb _settingsFromDb;

        public TemplateController(
            IMapper mapper,
            ITemplateRepository templateRepository,
            IRobotConfigurationRepository robotConfigurationRepository,
            IClientService clientService,
            ILogger<TemplateController> logger,
            IShortcodesService shortcodesService,
            IOrderRepository orderRepository,
            SettingsFromDb settingsFromDb
            )
        {
            _mapper = mapper;
            _templateRepository = templateRepository;
            _robotConfigurationRepository = robotConfigurationRepository;
            _clientService = clientService;
            _logger = logger;
            _shortcodesService = shortcodesService;
            _orderRepository = orderRepository;
            _settingsFromDb = settingsFromDb;
        }


        /// <summary>
        /// Получить список всех шаблонов постранично
        /// </summary>
        /// <param name="paginationQuery"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Template.GetAll)]
        public async Task<IActionResult> GetAll([FromQuery] PaginationQuery paginationQuery, [FromQuery] TemplateQuery filter)
        {
            var paginationFilter = _mapper.Map<PaginationFilter>(paginationQuery);

            IQueryable<Template> query = _templateRepository.Get()
                                            .AsNoTracking()
                                            .Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Field)
                                            .Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Value)
                                            .Include(t => t.StatusType)
                                            .Include(t => t.Job).ThenInclude(t => t.Group)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
                                            .OrderBy(t => t.Name);


            if (!string.IsNullOrEmpty(filter.Mask))
                query = query.Where(t => EF.Functions.Like(t.Name.ToLower(), SqlHelpers.RegexToLike(filter.Mask)));

            if (filter.JobId.HasValue)
                query = query.Where(t => t.JobId == filter.JobId);

            if (filter.StatusTypeId.HasValue)
                query = query.Where(t => t.StatusTypeId == filter.StatusTypeId);

            #region Статус синхронизации

            switch (filter.SyncStatus)
            {
                case (TemplateQuerySyncStatusEnum.Waiting):
                    query = query.Where(t => t.RobotConfigurations.Any(x => x.TaskStatusCode != (int)TaskStatusEnum.Ok));
                    break;
                case (TemplateQuerySyncStatusEnum.Complete):
                    query = query.Where(t => t.RobotConfigurations.All(x => x.TaskStatusCode == (int)TaskStatusEnum.Ok));
                    break;
                case (TemplateQuerySyncStatusEnum.Error):
                    query = query.Where(t => t.RobotConfigurations.Any(x => x.RobotStatusCode == (int)RobotStatusEnum.Error));
                    break;
                default:
                    break;
            }

            #endregion

            var templates = await _templateRepository.GetPage(query, paginationFilter).ToListAsync();

            //logger.LogDebug("Загрузка шаблонов из БД: {ElapsedMs} мс", sw.ElapsedMilliseconds);


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

            var templateResponse = _mapper.Map<List<TemplateListResponse>>(templates);

            // словари для быстрого поиска
            var templatesDict = templates.ToDictionary(t => t.Id);
            var templateResponseDict = templateResponse.ToDictionary(t => t.Id);

            #region Заполняем шорткоды и NextRunWithResponseAreaOffset
            if (filter.ApplyShortcode == true)
            {
                //sw.Restart();
                foreach (var responseItem in templateResponse)
                {
                    //await ApplyTemplateShortcodesAsync(responseItem, templates.First(t => t.Id == responseItem.Id));
                    await ApplyBaseTemplateShortcodesAsync(responseItem, templatesDict[responseItem.Id]);
                    //FillResponseAreaOffset(responseItem);
                }
                //logger.LogDebug("Получение шорткодов: {ElapsedMs} мс", sw.ElapsedMilliseconds);
                //sw.Stop();
            }
            #endregion

            #region заполнение OrdersCount
            var templatesWithOrdersCount = await GetOrdersCountAsync(templates.Select(t => t.Id).ToList());
            foreach (var (templateId, orderCount) in templatesWithOrdersCount)
            {
                if (templateResponseDict.TryGetValue(templateId, out var response))
                    response.OrderCount = orderCount;
            }
            #endregion

            var paginationResponse = new PagedResponse<TemplateListResponse>(templateResponse, true).GetPaginatedProps(paginationFilter, query);

            return Ok(paginationResponse);
        }

        /// <summary>
        /// Получить шаблон по id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Template.Get)]
        public async Task<IActionResult> GetById([FromRoute] Guid id)
        {
            var template = await _templateRepository.GetWithIncludes()
                                                        .AsNoTracking()
                                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
                                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
                                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
                                                            //.Include(t => t.Orders)
                                                            .Include(t => t.StatusType)
                                                            .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeType)
                                                            .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeTypeCalendar)
                                                        .AsSplitQuery()
                                                        .FirstOrDefaultAsync(t => t.Id == id);

            if (template == null)
                return NotFound();

            var response = _mapper.Map<TemplateResponse>(template);
            await ApplyBaseTemplateShortcodesAsync(response, template);
            await ApplyTemplateShortcodesAsync(response, template);

            var ordersCountResult = await GetOrdersCountAsync(new List<Guid> { response.Id });
            response.OrderCount = ordersCountResult.Count > 0 ? ordersCountResult.First().Value : 0;

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


        /// <summary>
        /// Изменить статус у шаблона по ИД (активировать/деактивировать)
        /// </summary>
        /// <param name="id">ИД шаблона</param>
        /// <returns></returns>
        [HttpPut(ApiRoutes.Template.ChangeState)]
        public async Task<IActionResult> ChangeState([FromRoute] Guid id, [FromBody] TemplateChangeStateRequest request)
        {
            //TODO:!!! Возможно тут не надо сразу менять, а отправить запрос в TemplateActivator и пусть он сам занимается своей работой!!!

            #region old
            //var template = await templateService.GetWithIncludes()
            //                                               //.Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Field)
            //                                               //.Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Value)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
            //                                               .Include(t => t.Orders)
            //                                               .Include(t => t.StatusType)
            //                                               .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeType)
            //                                               .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeTypeCalendar)
            //                                           .AsSplitQuery()
            //                                           .FirstOrDefaultAsync(t => t.Id == id);
            #endregion

            var template = await _templateRepository.Get()
                                                    .Include(t => t.RobotConfigurations)
                                                    .FirstOrDefaultAsync(t => t.Id == id);

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

            if (template.IsActiveTemplate != request.IsActiveTemplate)
            {
                template.IsActiveTemplate = request.IsActiveTemplate;
                //необходимо обновить шаблон
                var config = _robotConfigurationRepository.GetFromTemplateByRobotCode(RobotsEnum.TemplateOrder, template);
                //robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, config);
                _robotConfigurationRepository.SetUpdateTaskStatusIfAllow(config);
            }

            if (template.IsActiveSchedule != request.IsActiveSchedule)
            {
                template.IsActiveSchedule = request.IsActiveSchedule;
                //необходимо обновить расписание
                var config = _robotConfigurationRepository.GetFromTemplateByRobotCode(RobotsEnum.ScheduleOrder, template);
                //robotConfigurationService.ChangeTaskStatus(TaskStatusEnum.Updating, config);
                _robotConfigurationRepository.SetUpdateTaskStatusIfAllow(config);
            }

            if (!await _templateRepository.CommitAsync(new HistoryInitiator { InitiatorComment = "Изменён статус шаблона/расписания", InitiatorIp = _clientService.GetClientIp()?.ToString(), InitiatorParrComponentId = ParrComponentsEnum.Api }))
                return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при изменении шаблона." } }));

            #region old
            //var templateToResponse = await templateService.GetWithIncludes().AsNoTracking()
            //                                               //.Include(t => t.Unit).ThenInclude(t => t.UnitValues).ThenInclude(t => t.Field)
            //                                               //.Include(t => t.Unit).ThenInclude(t => t.UnitValues).ThenInclude(t => t.Value)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
            //                                               .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
            //                                               .Include(t => t.Orders)
            //                                               .Include(t => t.StatusType)
            //                                               .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeType)
            //                                               .Include(t => t.Job).ThenInclude(t => t.Group).ThenInclude(t => t.ScheduleExcludeTypeCalendar)
            //                                           .AsSplitQuery()
            //                                           .FirstAsync(t => t.Id == id);

            //var response = mapper.Map<TemplateResponse>(templateToResponse);
            //await ApplyTemplateShortcodesAsync(response, templateToResponse);

            //return Ok(new Response<TemplateResponse>(response, true));
            #endregion

            var templateToResponse = await _templateRepository.Get()
                                            .AsNoTracking()
                                            .Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Field)
                                            .Include(t => t.Unit).ThenInclude(t => t!.UnitValues).ThenInclude(t => t.Value)
                                            .Include(t => t.StatusType)
                                            .Include(t => t.Job).ThenInclude(t => t.Group)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.Robot)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.TaskStatus)
                                            .Include(t => t.RobotConfigurations).ThenInclude(t => t.RobotStatus)
                                            .FirstAsync(t => t.Id == id);

            var response = _mapper.Map<TemplateListResponse>(templateToResponse);

            await ApplyBaseTemplateShortcodesAsync(response, templateToResponse);

            var ordersCountResult = await GetOrdersCountAsync(new List<Guid> { response.Id });
            response.OrderCount = ordersCountResult.Count > 0 ? ordersCountResult.First().Value : 0;

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


        /// <summary>
        /// Применить шорткоды. Для респонса TemplateBaseResponse
        /// </summary>
        /// <param name="response"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        private async Task ApplyBaseTemplateShortcodesAsync(TemplateBaseResponse response, Template template)
        {
            response.WorkGroup = await _shortcodesService.ApplyShortcodesAsync(template.Job!.WorkGroupMask, template);
            response.ResponseArea = await _shortcodesService.ApplyShortcodesAsync(template.Job!.ResponseAreaMask, template);
        }


        /// <summary>
        /// Применить шорткоды. Для респонса TemplateResponse (добавлены дополнительные поля Rendered)
        /// </summary>
        /// <param name="response"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        private async Task ApplyTemplateShortcodesAsync(TemplateResponse response, Template template)
        {
            response.WorkNameRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job!.WorkName, template);
            response.WorkGroupRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job!.WorkGroupMask, template);
            response.ResponseAreaRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job!.ResponseAreaMask, template);
            response.ShortDescriptionRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job.Group!.ShortDescription, template);
            response.FullDescriptionRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job.Group!.FullDescription, template);
            response.SolutionRendered = await _shortcodesService.ApplyShortcodesAsync(template.Job.Group!.Solution, template);
        }

        /// <summary>
        /// Получить кол-во нарядов для шаблонов
        /// </summary>
        /// <param name="templateIdList"></param>
        /// <returns></returns>
        private async Task<Dictionary<Guid, int>> GetOrdersCountAsync(List<Guid> templateIdList)
        {
            if (!templateIdList.Any())
                return new Dictionary<Guid, int>();

            var templateWithOrders = await _orderRepository.Get()
                                                .Where(t => t.TemplateId.HasValue && templateIdList.Contains(t.TemplateId.Value))
                                                .GroupBy(t => t.TemplateId)
                                                .Select(t => new { TemplateId = t.Key, OrderCount = t.Count() })
                                                .AsNoTracking()
                                                .ToListAsync();

            if (templateWithOrders == null)
                return new Dictionary<Guid, int>();

            return templateWithOrders.ToDictionary(t => t.TemplateId!.Value, t => t.OrderCount);
        }




    }
}
