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.Base;
using PARR.API.Contracts.V1.Responses.Statistics;
using PARR.API.Controllers.V1.Base;
using PARR.Core.Services.RobotMetrics;
using PARR.Domain.Common.Roles;
using PARR.Domain.DTOs.RobotMetrics;
using PARR.Domain.Enums;

namespace PARR.API.Controllers.V1.Statistics
{
    /// <summary>
    /// Метрики заданий роботам
    /// </summary>
    [Authorize(Roles = ParrRoles.Administrator.Role)]
    public class StatRobotMetricsController : BaseApiController
    {
        private readonly IMapper _mapper;
        private readonly IRobotMetricsService _robotMetricsService;

        public StatRobotMetricsController(
            IMapper mapper,
            IRobotMetricsService robotMetricsService
            )
        {
            _mapper = mapper;
            _robotMetricsService = robotMetricsService;
        }


        /// <summary>
        /// Статистика по Заданиям Роботу, график
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.StatRobotMetrics.GetRobotStatusMetrics)]
        public async Task<IActionResult> GetRobotStatusMetrics([FromRoute] RobotsEnum robotCode, [FromRoute] ChartPeriod period, CancellationToken cancellationToken)
        {
            var data = await _robotMetricsService.GetRobotStatusMetricsAsync(robotCode, period, cancellationToken);

            var response = _mapper.Map<List<StatRobotStatusChartPoint>>(data);

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


        /// <summary>
        /// Статистика по Статусам Заданий, график
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.StatRobotMetrics.GetTaskStatusMetrics)]
        public async Task<IActionResult> GetTaskStatusMetrics([FromRoute] RobotsEnum robotCode, [FromRoute] ChartPeriod period, CancellationToken cancellationToken)
        {
            var data = await _robotMetricsService.GetTaskStatusMetricsAsync(robotCode, period, cancellationToken);

            var response = _mapper.Map<List<StatTaskStatusChartPoint>>(data);

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


        /// <summary>
        /// Статистика по статусам заданий, гибкий фильтр
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.StatRobotMetrics.GetFilteredMetrics)]
        public async Task<IActionResult> GetFilteredMetrics([FromQuery] RobotFilteredMetricsQuery request, CancellationToken cancellationToken)
        {
            // ------- Правильность расчетов этого метода доконца не проверена ------- 

            var filter = _mapper.Map<MetricFilter>(request);

            var data = await _robotMetricsService.GetFilteredMetricsAsync(filter, cancellationToken);

            var response = _mapper.Map<List<StatFilteredChartPoint>>(data);

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

    }
}
