using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using WeatherForecast.Services;
using DomainResults.Mvc;

namespace WeatherForecast.Controllers;

/// <summary>
/// 
/// </summary>
[AllowAnonymous]
[Route("api/[controller]")]
[ApiController]
public class ImageController : ControllerBase {

  private readonly IImageService _imageService;

  /// <summary>
  /// 
  /// </summary>
  /// <param name="imgeService"></param>
  public ImageController(
    IImageService imgeService
  ) {
    _imageService = imgeService;
  }

  /// <summary>
  /// 
  /// </summary>
  /// <param name="siteId"></param>
  /// <param name="width"></param>
  /// <param name="height"></param>
  /// <param name="imageId"></param>
  /// <returns></returns>
  [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
    };
  }

}
