using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests.BaseRequests;
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.Workload.Interfaces;
using PARR.Domain.Common.Roles;
using PARR.Domain.Enums.Workload;

namespace PARR.API.Controllers.V1.Statistics
{
    /// <summary>
    /// Статситика загруженности - шаблоны
    /// </summary>
    [Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
    public class StatWorkloadTemplateController : BaseApiController
    {
        private readonly IMapper mapper;
        private readonly IWorkloadService workloadService;
        private readonly ILogger<StatWorkloadTemplateController> logger;

        public StatWorkloadTemplateController(
            IMapper mapper,
            IWorkloadService workloadService,
            ILogger<StatWorkloadTemplateController> logger
            )
        {
            this.mapper = mapper;
            this.workloadService = workloadService;
            this.logger = logger;
        }


        /// <summary>
        /// Отчет о загруженности. Шаблоны
        /// </summary>
        /// <param name="reportType">Тип отчета (ЗО, РГ, Работа)</param>
        /// <param name="filter">Фильтр, значиение в зависимости от типа отчета</param>
        /// <param name="state">Все, активированные, деактивированные</param>
        /// <param name="date">Дата</param>
        /// <param name="offset">Часовой пояс</param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        [HttpGet(ApiRoutes.StatWorkloadTemplate.GetWorkloadTemplateReport)]
        public async Task<IActionResult> GetWorkloadTemplateReport(
            [FromRoute] WorkloadTemplateReportType reportType,
            [FromRoute] string filter,
            [FromRoute] WorkloadTemplateReportState state,
            [FromRoute] DateOnly date,
            [FromQuery] TimeZoneOffsetClient offset,
            [FromQuery] StatWorkloadTemplateReqportQuery query
            )
        {
            if (string.IsNullOrWhiteSpace(filter))
            {
                logger.LogWarning("Не передано значение фильтра");
                throw new ArgumentException(nameof(filter), "Не передано значение фильтра.");
            }

            var report = await workloadService.GetTemplateReportAsync(reportType, filter.Trim(), state, date, offset.TimeZoneOffset, query.ResponseArea?.Trim(), query.WorkGroup?.Trim());

            var response = mapper.Map<List<StatWorkloadTemplateReportResponse>>(report);

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


    }
}
