using Amazon.S3.Model;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using User.Insight.Tracker.Common;
using User.Insight.Tracker.DataContracts;
using User.Insight.Tracker.DataContracts.Requests;
using User.Insight.Tracker.DataContracts.Response;
using User.Insight.Tracker.Services;

namespace User.Insight.Tracker.Controllers
{
    [ApiController]
    [ExcludeFromCodeCoverage]
    [Route("")]
    public class S3BucketController : ApiControllerBase
    {
        private readonly ILogger<S3BucketController> _logger;
        private readonly IS3BucketService _s3Bucket;

        public S3BucketController(ILogger<S3BucketController> logger, IS3BucketService s3Bucket, IHttpContextAccessor httpContextAccessor)
            : base(httpContextAccessor)
        {
            _logger = logger;
            _s3Bucket = s3Bucket;
        }

        [ExcludeFromCodeCoverage]
        [HttpPost]
        [Route("ScreenShotList")]
        [CustomExceptionFilter]
        public async Task<ActionResult<Payload<PageResponse<S3BucketResponse>>>> ListImages(SearchRequest<S3BucketRequest> s3Bucket)
        {
            _logger.LogInformation("S3BucketController.ScreenShotList()");

            // Ensure valid pagination parameters
            if (s3Bucket.PageNumber < 1) s3Bucket.PageNumber = 1;
            if (s3Bucket.PageSize < 1) s3Bucket.PageSize = 10;

            var paginatedData = await _s3Bucket.GetImageUrlsFromS3Async(s3Bucket, CurrentUserJWT); // Use await here

            var response = new Payload<PageResponse<S3BucketResponse>>
            {
                Data = paginatedData,
                Message = Constant.MsgSuccess,
                IsSuccess = true
            };

            return StatusCode((int)HttpStatusCode.OK, response);
        }

    }
}
