const express = require("express")
const router = express.Router()

const requireAuth = require("../middleware/requireAuth")
const requireRole = require("../middleware/requireRole")
const { validate } = require("../middleware/validate.middleware")
const { endpoints } = require("@shared/contracts")
const { getPendingDeals, approveDeal, rejectDeal, expireDeal, unexpireDeal } = require("../services/mod.service")
const { mapPaginatedDealsToDealCardResponse } = require("../adapters/responses/dealCard.adapter")

const { deals } = endpoints

const listQueryValidator = validate(deals.dealsListRequestSchema, "query", "validatedDealListQuery")

const buildViewer = (req) =>
  req.auth ? { userId: req.auth.userId, role: req.auth.role } : null

router.get("/deals/pending", requireAuth, requireRole("MOD"), listQueryValidator, async (req, res) => {
  try {
    const { q, page, limit } = req.validatedDealListQuery
    const payload = await getPendingDeals({
      page,
      limit,
      filters: { ...req.query, q },
      viewer: buildViewer(req),
    })

    const response = mapPaginatedDealsToDealCardResponse(payload)
    res.json(response.results)
  } catch (err) {
    const status = err.statusCode || 500
    res.status(status).json({ error: err.message || "Sunucu hatasi" })
  }
})

router.post(
  "/deals/:id/approve",
  requireAuth,
  requireRole("MOD"),
  validate(deals.dealDetailRequestSchema, "params", "validatedDealId"),
  async (req, res) => {
    try {
      const { id } = req.validatedDealId
      const updated = await approveDeal(id)
      res.json({ id: updated.id, status: updated.status })
    } catch (err) {
      const status = err.statusCode || 500
      res.status(status).json({ error: err.message || "Sunucu hatasi" })
    }
  }
)

router.post(
  "/deals/:id/reject",
  requireAuth,
  requireRole("MOD"),
  validate(deals.dealDetailRequestSchema, "params", "validatedDealId"),
  async (req, res) => {
    try {
      const { id } = req.validatedDealId
      const updated = await rejectDeal(id)
      res.json({ id: updated.id, status: updated.status })
    } catch (err) {
      const status = err.statusCode || 500
      res.status(status).json({ error: err.message || "Sunucu hatasi" })
    }
  }
)

router.post(
  "/deals/:id/expire",
  requireAuth,
  requireRole("MOD"),
  validate(deals.dealDetailRequestSchema, "params", "validatedDealId"),
  async (req, res) => {
    try {
      const { id } = req.validatedDealId
      const updated = await expireDeal(id)
      res.json({ id: updated.id, status: updated.status })
    } catch (err) {
      const status = err.statusCode || 500
      res.status(status).json({ error: err.message || "Sunucu hatasi" })
    }
  }
)

router.post(
  "/deals/:id/unexpire",
  requireAuth,
  requireRole("MOD"),
  validate(deals.dealDetailRequestSchema, "params", "validatedDealId"),
  async (req, res) => {
    try {
      const { id } = req.validatedDealId
      const updated = await unexpireDeal(id)
      res.json({ id: updated.id, status: updated.status })
    } catch (err) {
      const status = err.statusCode || 500
      res.status(status).json({ error: err.message || "Sunucu hatasi" })
    }
  }
)

module.exports = router
