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

namespace Insight.Api.Controllers
{
    [ApiController, Route("api/agents")]
    public class AgentController : ControllerBase
    {
        private readonly AgentService _agentService;
        private readonly ILogger<AgentController> _logger;

        public AgentController(AgentService agentService, ILogger<AgentController> logger)
        {
            _agentService = agentService;
            _logger = logger;
        }

        [HttpGet, Authorize]
        public async Task<IActionResult> Get([FromQuery] PagedDataRequest request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _agentService.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);
            }
        }
    }
}