using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace AsbCloudWebApi.Controllers;

/// <summary>
/// Секции скважины
/// </summary>
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class WellSectionsController : ControllerBase
{
    private readonly IWellOperationRepository wellOperationRepository;

    public WellSectionsController(IWellOperationRepository wellOperationRepository)
    {
        this.wellOperationRepository = wellOperationRepository;
    }

    /// <summary>
    /// Получение списка плановых секций скважин
    /// </summary>
    /// <param name="idsWells">Идентификаторы скважин</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpGet("plan")]
    [ProducesResponseType(typeof(IEnumerable<SectionByOperationsDto>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetPlanAsync([FromQuery] IEnumerable<int> idsWells,
        CancellationToken cancellationToken)
    {
        var sections = await wellOperationRepository.GetSectionsAsync(idsWells, cancellationToken);
        sections = sections.Where(section => section.IdType == 0);
        return Ok(sections);
    }

    /// <summary>
    /// Получение списка фактических секций скважин
    /// </summary>
    /// <param name="idsWells">Идентификаторы скважин</param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [HttpGet("fact")]
    [ProducesResponseType(typeof(IEnumerable<SectionByOperationsDto>), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetFactAsync([FromQuery] IEnumerable<int> idsWells,
        CancellationToken cancellationToken)
    {
        var sections = await wellOperationRepository.GetSectionsAsync(idsWells, cancellationToken);
        sections = sections.Where(section => section.IdType == 1);
        return Ok(sections);
    }
}