using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using WeatherForecast.Services;
using DomainResults.Mvc;
namespace WeatherForecast.Controllers;
/// 
/// 
/// 
[Route("api/[controller]")]
[ApiController]
public class ImageController : ControllerBase {
  private readonly IAuthorizationService _authorizationService;
  private readonly IImageService _imageService;
  /// 
  /// 
  /// 
  /// 
  /// 
  public ImageController(
    IAuthorizationService authorizationService,
    IImageService imgeService
  ) {
    _authorizationService = authorizationService;
    _imageService = imgeService;
  }
  #region Authless methods
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  /// 
  [HttpGet("{siteId}/{width}x{height}/{imageId}")]
  public IActionResult Get([FromRoute] Guid siteId, [FromRoute] int width, [FromRoute] int height, [FromRoute] Guid imageId) {
    var (file, result) = _imageService.Get(siteId, width, height, imageId);
    if (!result.IsSuccess || file == null)
      return result.ToActionResult();
    var stream = new MemoryStream(file.Bytes);
    return new FileStreamResult(stream, file.ContentType) {
      FileDownloadName = file.Name
    };
  }
  #endregion
}