using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Persistence.Models;
using Persistence.Repositories;
using System.Net;

namespace Persistence.API.Controllers;

/// <summary>
/// Работа с уставками
/// </summary>
[ApiController]
[Authorize]
[Route("api/[controller]")]
public class SetpointController : ControllerBase, ISetpointApi
{
    private readonly ISetpointRepository setpointRepository;

    public SetpointController(ISetpointRepository setpointRepository)
    {
        this.setpointRepository = setpointRepository;
    }

    /// <summary>
    /// Получить актуальные значения уставок
    /// </summary>
    /// <param name="setpointKeys"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("current")]
    public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetCurrent([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
    {
        var result = await setpointRepository.GetCurrent(setpointKeys, token);

        return Ok(result);
    }

    /// <summary>
    /// Получить значения уставок за определенный момент времени
    /// </summary>
    /// <param name="setpointKeys"></param>
    /// <param name="historyMoment"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("history")]
    public async Task<ActionResult<IEnumerable<SetpointValueDto>>> GetHistory([FromQuery] IEnumerable<Guid> setpointKeys, [FromQuery] DateTimeOffset historyMoment, CancellationToken token)
    {
        var result = await setpointRepository.GetHistory(setpointKeys, historyMoment, token);

        return Ok(result);
    }

    /// <summary>
    /// Получить историю изменений значений уставок
    /// </summary>
    /// <param name="setpointKeys"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("log")]
    public async Task<ActionResult<Dictionary<Guid, IEnumerable<SetpointLogDto>>>> GetLog([FromQuery] IEnumerable<Guid> setpointKeys, CancellationToken token)
    {
        var result = await setpointRepository.GetLog(setpointKeys, token);

        return Ok(result);
    }

    /// <summary>
    /// Получить диапазон дат, для которых есть данные в репозитории
    /// </summary>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("range")]
    public async Task<ActionResult<DatesRangeDto>> GetDatesRangeAsync(CancellationToken token)
    {
        var result = await setpointRepository.GetDatesRangeAsync(token);

        return Ok(result);
    }

    /// <summary>
    /// Получить порцию записей, начиная с заданной даты 
    /// </summary>
    /// <param name="dateBegin"></param>
    /// <param name="take"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpGet("part")]
    public async Task<ActionResult<IEnumerable<SetpointLogDto>>> GetPart(DateTimeOffset dateBegin, int take, CancellationToken token)
    {
        var result = await setpointRepository.GetPart(dateBegin, take, token);

        return Ok(result);
    }

    /// <summary>
    /// Сохранить уставку
    /// </summary>
    /// <param name="setpointKey"></param>
    /// <param name="newValue"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    [HttpPost]
    [ProducesResponseType(typeof(int), (int)HttpStatusCode.Created)]
    public async Task<IActionResult> Add(Guid setpointKey, object newValue, CancellationToken token)
    {
        var userId = User.GetUserId<Guid>();
        await setpointRepository.Add(setpointKey, newValue, userId, token);

        return CreatedAtAction(nameof(Add), true);
    }
}