using Insight.Infrastructure.Models;
using Insight.Infrastructure.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Insight.Api.Controllers
{
    [ApiController, Route("api/hosts")]
    public class HostController : ControllerBase
    {
        private readonly HostService _hostService;
        private readonly ILogger<HostController> _logger;

        public HostController(HostService hostService, ILogger<HostController> logger)
        {
            _hostService = hostService;
            _logger = logger;
        }

        [HttpGet, Authorize]
        public async Task<IActionResult> Get([FromQuery] PagedDataRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _hostService.GetAsync(
                offset: request.Offset,
                limit: request.Limit,
                request: Request,
                response: Response,
                cancellationToken: cancellationToken).ConfigureAwait(false);

                return Ok(result);
            }
            catch (UnauthorizedAccessException ex)
            {
                return Unauthorized(ex.ToString());
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}