using AsbCloudApp.Data;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.WellOperation;

namespace AsbCloudWebApi.Controllers;


/// <summary>
/// Статистика по операциям (заведенным вручную) на скважине
/// </summary>
[ApiController]
[Authorize]
[Route("api")]
public class OperationStatController : ControllerBase
{
    private readonly IOperationsStatService operationsStatService;
    private readonly IWellOperationService wellOperationService;
    private readonly IWellService wellService;

    public OperationStatController(
        IOperationsStatService operationsStatService, 
        IWellService wellService,
        IWellOperationService wellOperationService)
    {
        this.operationsStatService = operationsStatService;
        this.wellService = wellService;
        this.wellOperationService = wellOperationService;
    }

    /// <summary>
    /// Формирует данные по среднему и максимальному МСП на кусту по id скважины
    /// </summary>
    /// <param name="idWell">id скважины с данного куста (через нее будут получены данные)</param>
    /// <param name="token"></param>
    /// <returns>Возвращает данные по среднему и максимальному МСП на кусту</returns>
    [HttpGet("well/{idWell}/ropStat")]
    [Permission]
    [ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetClusterRopStatByIdWellAsync([FromRoute] int idWell,
        CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();

        var result = await operationsStatService.GetOrDefaultRopStatAsync(
            idWell, token).ConfigureAwait(false);

        return Ok(result);
    }

    /// <summary>
    /// Формирует данные по среднему и максимальному МСП на кусту по uid панели
    /// </summary>
    /// <param name="uid">id передающей данные панели</param>
    /// <param name="token"></param>
    /// <returns>Возвращает данные по среднему и максимальному МСП на кусту</returns>
    [HttpGet("telemetry/{uid}/ropStat")]
    [ProducesResponseType(typeof(ClusterRopStatDto), (int)System.Net.HttpStatusCode.OK)]
    [AllowAnonymous]
    public async Task<IActionResult> GetClusterRopStatByUidAsync([FromRoute] string uid,
        CancellationToken token)
    {
        var idWell = wellService.TelemetryService.GetIdWellByTelemetryUid(uid);

        if (idWell is null)
            return NoContent();

        var result = await operationsStatService.GetOrDefaultRopStatAsync(
            (int)idWell, token).ConfigureAwait(false);

        return Ok(result);
    }

    /// <summary>
    /// Получает статистику по скважинам куста
    /// </summary>
    /// <param name="idCluster">id куста</param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("cluster/{idCluster}/stat")]
    [Permission]
    [ProducesResponseType(typeof(StatClusterDto), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetStatClusterAsync(int idCluster,
        CancellationToken token)
    {
        int? idCompany = User.GetCompanyId();
        if (idCompany is null)
            return Forbid();

        var result = await operationsStatService.GetOrDefaultStatClusterAsync(idCluster, idCompany.Value, token)
            .ConfigureAwait(false);
        return Ok(result);
    }

    /// <summary>
    /// Получает статистику по списку скважин
    /// </summary>
    /// <param name="idWells">список скважин</param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("wellsStats")]
    [Permission]
    [ProducesResponseType(typeof(IEnumerable<StatWellDto>), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetWellsStatAsync([FromQuery] IEnumerable<int> idWells, CancellationToken token)
    {
        int? idCompany = User.GetCompanyId();
        if (idCompany is null)
            return Forbid();

        var allowedWells = await wellService.GetAsync(new() { IdCompany = idCompany }, token);

        var protectedIdWells = idWells.Where(id => allowedWells.Any(allowed => allowed.Id == id));
        var result = await operationsStatService.GetWellsStatAsync(protectedIdWells, token)
            .ConfigureAwait(false);
        return Ok(result);
    }

    /// <summary>
    /// Получает статистику по скважине
    /// </summary>
    /// <param name="idWell">id скважины</param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("well/{idWell}/stat")]
    [Permission]
    [ProducesResponseType(typeof(StatWellDto), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetStatWellAsync(int idWell,
        CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();

        var result = await operationsStatService.GetOrDefaultWellStatAsync(idWell, token)
            .ConfigureAwait(false);
        return Ok(result);
    }

    /// <summary>
    /// Получает данные для графика глубина-день
    /// </summary>
    /// <param name="idWell"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("well/{idWell}/tvd")]
    [Permission]
    [ProducesResponseType(typeof(IEnumerable<PlanFactPredictBase<WellOperationDto>>), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetTvdAsync(int idWell,
        CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();
        
        var result = await wellOperationService.GetTvdAsync(idWell, token);
        return Ok(result);
    }

    private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token)
    {
        int? idCompany = User.GetCompanyId();
        return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
            idWell, token).ConfigureAwait(false);
    }
}