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

namespace Insight.Api.Controllers
{
    [ApiController, Route("api/customers")]
    public class CustomerController : ControllerBase
    {
        private readonly CustomerService _customerService;
        private readonly ILogger<CustomerController> _logger;

        public CustomerController(CustomerService customerService, ILogger<CustomerController> logger)
        {
            _customerService = customerService;
            _logger = logger;
        }

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