const express = require("express")
const { getCachedImageByKey } = require("../services/redis/linkPreviewImageCache.service")

const router = express.Router()

router.get("/deal_create/:key", async (req, res) => {
  try {
    const key = req.params.key
    const cached = await getCachedImageByKey(key)
    if (!cached) return res.status(404).json({ error: "Not found" })
    res.setHeader("Content-Type", cached.contentType)
    res.setHeader("Cache-Control", "public, max-age=300")
    return res.status(200).send(cached.buffer)
  } catch (err) {
    return res.status(500).json({ error: "Sunucu hatasi" })
  }
})

module.exports = router
