const express = require("express")
const authRequiredMiddleware = require("../middleware/authRequired.middleware")
const authOptionalMiddleware = require("../middleware/authOptional.middleware")
const voteService = require("../services/vote.service")
const {mapVoteRequestToVoteInput,mapVoteResultToResponse}=require("../adapters/responses/vote.adapter")
const router = express.Router()


router.post("/", authRequiredMiddleware
, async (req, res) => {
  try {
    const input = mapVoteRequestToVoteInput(req);
    const result = await voteService.voteDeal(input);
    res.json(result);
  } catch (err) {
    const status = err.statusCode || 500;
    res.status(status).json({ message: err.message || "Sunucu hatası" });
  }
});
// Belirli deal için oyları çek
router.get("/:dealId", async (req, res) => {
  try {
    const dealId = Number(req.params.dealId)
    if (isNaN(dealId) || dealId <= 0)
      return res.status(400).json({ error: "Geçersiz dealId" })

    const data = await voteService.getVotes(dealId)
    res.json(data)
  } catch (err) {
    console.error(err)
    res.status(500).json({ error: "Sunucu hatası" })
  }
})

module.exports = router
