package de.grimsi.gameyfin.rest;

import de.grimsi.gameyfin.service.DownloadService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * This controller handles functionality for images.
 */
@RestController
@RequestMapping("/v1/images")
@RequiredArgsConstructor
public class ImageController {

    private final DownloadService downloadService;

    @GetMapping(value = "/{imageId}", produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<Resource> getCoverImageForGame(@PathVariable String imageId) {
        return ResponseEntity.ok()
                .cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS).cachePublic())
                .body(downloadService.sendImageToClient(imageId));
    }
}
