/**
 * gatewayController.js
 * ────────────────────
 * Only handles HTTP req/res.
 * All logic is delegated to gatewayService.
 */

const GatewayService = require("../services/gatewayService");

// ─── Helper ──────────────────────────────────────────────────────────────────
function handleError(res, err) {
  if (err.status) {
    return res.status(err.status).json({ success: false, error: err.message });
  }
  console.error(err);
  return res.status(500).json({ success: false, error: "Internal server error" });
}

// ─── Controllers ─────────────────────────────────────────────────────────────

// GET /api/gateways
async function getAllGateways(req, res) {
  try {
    const data = await GatewayService.getAllGateways();
    res.json({ success: true, count: data.length, data });
  } catch (err) { handleError(res, err); }
}

// GET /api/gateways/status/active
async function getActiveGateways(req, res) {
  try {
    const data = await GatewayService.getActiveGateways();
    res.json({
      success: true,
      count:   data.length,
      data,
      message: data.length === 0 ? "No payment gateways are currently active" : undefined,
    });
  } catch (err) { handleError(res, err); }
}

// GET /api/gateways/:id
async function getGateway(req, res) {
  try {
    const data = await GatewayService.getGatewayByKey(req.params.id);
    res.json({ success: true, data });
  } catch (err) { handleError(res, err); }
}

// POST /api/gateways
async function createGateway(req, res) {
  try {
    const data = await GatewayService.createGateway(req.body);
    res.status(201).json({ success: true, message: `Gateway '${data.gateway_key}' created`, data });
  } catch (err) { handleError(res, err); }
}

// PATCH /api/gateways/:id
async function updateGateway(req, res) {
  try {
    const data = await GatewayService.updateGateway(req.params.id, req.body);
    res.json({ success: true, message: `Gateway '${req.params.id}' updated`, data });
  } catch (err) { handleError(res, err); }
}

// POST /api/gateways/:id/toggle
async function toggleGateway(req, res) {
  try {
    const data = await GatewayService.toggleGateway(req.params.id, req.body?.updated_by);
    const state = data.is_enabled ? "enabled" : "disabled";
    res.json({ success: true, message: `Gateway '${req.params.id}' ${state}`, data });
  } catch (err) { handleError(res, err); }
}

// DELETE /api/gateways/:id
async function deleteGateway(req, res) {
  try {
    const result = await GatewayService.deleteGateway(req.params.id, req.body?.updated_by);
    res.json({ success: true, ...result });
  } catch (err) { handleError(res, err); }
}

// GET /api/gateways/audit
async function getAuditLog(req, res) {
  try {
    const limit   = parseInt(req.query.limit) || 50;
    const gateway = req.query.gateway || null;
    const data    = await GatewayService.getAuditLog(gateway, limit);
    res.json({ success: true, count: data.length, data });
  } catch (err) { handleError(res, err); }
}

// POST /api/gateways/reset
async function resetGateways(req, res) {
  try {
    const result = await GatewayService.resetAllGateways(req.body?.updated_by);
    res.json({ success: true, ...result });
  } catch (err) { handleError(res, err); }
}

module.exports = {
  getAllGateways,
  getActiveGateways,
  getGateway,
  createGateway,
  updateGateway,
  toggleGateway,
  deleteGateway,
  getAuditLog,
  resetGateways,
};
