using Lactose.Repositories;
using Lactose.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Lactose.Controllers;

[ApiController]
[Authorize]
[AllowAnonymous]
[Route("api/[controller]")]
public class MediaController(
    //TODO: Implement logging
    //ILogger<MediaController> logger
    LactoseAuthService authService,
    IMediaRepository mediaRepository
) : ControllerBase {
    

    [HttpGet("{id}")]
    [HttpGet("original/{id}")]
    public ActionResult GetImage(Guid id) {
        var user        = authService.GetUserData(User);

        var data = mediaRepository.GetOriginalData(id, user?.Id);
        if (data == null) return NotFound();

        return PhysicalFile(data.Path, data.MimeType);
    }

    [HttpGet("thumb/{id}")]
    public ActionResult GetThumb(Guid id) {
        var user        = authService.GetUserData(User);

        var data = mediaRepository.GetThumbData(id, user?.Id);
        if (data == null) return NotFound();

        return PhysicalFile(data.Path, data.MimeType);
    }

    [HttpGet("preview/{id}")]
    public ActionResult GetPreview(Guid id) {
        var user        = authService.GetUserData(User);

        var data = mediaRepository.GetPreviewData(id, user?.Id);
        if (data == null) return NotFound();

        return PhysicalFile(data.Path, data.MimeType);
    }
}