using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetworkResurrector.Api.Application.Queries;
using NetworkResurrector.Api.Authorization.Constants;
using NetworkResurrector.Api.PublishedLanguage.Commands;
using System;
using System.Threading.Tasks;

namespace NetworkResurrector.Api.Controllers
{
    [Authorize]
    [ApiController]
    [Route("system")]
    public class SystemController : ControllerBase
    {
        private readonly IMediator _mediator;

        public SystemController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [AllowAnonymous]
        [HttpGet("ping")]
        public IActionResult Ping()
        {
            return Ok($"Ping success. System datetime: {DateTime.Now}");
        }

        [HttpGet("version")]
        public async Task<IActionResult> GetSystemVersion([FromRoute] GetSystemVersion.Query query)
        {
            var result = await _mediator.Send(query);
            return Ok(result);
        }

        [HttpGet("release-notes")]
        public async Task<IActionResult> GetReleaseNotes([FromRoute] GetReleaseNotes.Query query)
        {
            var result = await _mediator.Send(query);
            return Ok(result);
        }

        [HttpPost("reset-cache")]
        [Authorize(Policy = Policies.SystemAdministration)]
        public async Task<IActionResult> ResetCache()
        {
            var resetCache = new ResetCache();
            var result = await _mediator.Send(resetCache);
            return Ok(result);
        }
    }
}
