using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace AsbCloudWebApi.Controllers;


/// <summary>
/// Композитная скважина
/// </summary>
[Route("api/well/{idWell}/composite")]
[ApiController]
[Authorize]
public class WellCompositeController : ControllerBase
{
    private readonly IWellCompositeRepository wellCompositeRepository;
    private readonly IWellService wellService;

    public WellCompositeController(IWellCompositeRepository wellCompositeRepository,
        IWellService wellService)
    {
        this.wellCompositeRepository = wellCompositeRepository;
        this.wellService = wellService;
    }

    /// <summary>
    /// Получает композитную скважину для скважины
    /// </summary>
    /// <param name="idWell">id скважины для которой собрана композитная скважина</param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet]
    [Permission]
    [ProducesResponseType(typeof(IEnumerable<WellCompositeDto>), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetAsync(int idWell, CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();

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

    /// <summary>
    /// Создает или заменяет композитную скважину для скважины с idWell
    /// </summary>
    /// <param name="idWell">id скважины для которой собрана композитная скважина</param>
    /// <param name="wellComposites">Секции композитной скважины</param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpPost]
    [Permission]
    public async Task<IActionResult> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();

        var result = await wellCompositeRepository.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false);
        return Ok(result);
    }

    /// <summary>
    /// Получение РТК по композитной скважине
    /// </summary>
    /// <param name="idWell"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("compositeProcessMap")]
    [Permission]
    [ProducesResponseType(typeof(IEnumerable<ProcessMapPlanBaseDto>), (int)System.Net.HttpStatusCode.OK)]
    public async Task<IActionResult> GetCompositeProcessMap(int idWell, CancellationToken token)
    {
        if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
            return Forbid();

        var result = await wellCompositeRepository.GetCompositeProcessMap(idWell, token).ConfigureAwait(false);
        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);
    }
}