'use strict';

const express = require('express');
const router = express.Router();
const svc = require('./arkhon-card.service');
const { requirePermission } = require('../../middleware/auth');

const ok = (res, data) => res.json({ ok: true, data });
const asyncRoute = (handler) => (req, res, next) => Promise.resolve(handler(req, res, next)).catch(next);
const actor = (req) => req.user?.usuario || req.user?.nombre || req.user?.id || 'arkhon';
const requireWriter = requirePermission('config.edit');

router.get('/status', asyncRoute(async (_req, res) => ok(res, await svc.status())));
router.get('/dashboard', asyncRoute(async (_req, res) => ok(res, await svc.dashboard())));
router.get('/config', asyncRoute(async (_req, res) => ok(res, await svc.getConfig())));
router.post('/config', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.saveConfig(req.body || {}, actor(req)))));
router.get('/map', asyncRoute(async (_req, res) => ok(res, svc.behaviorMap())));
router.get('/schema/restcard', asyncRoute(async (_req, res) => ok(res, await svc.restCardSchema())));
router.get('/schema/softrestaurant', asyncRoute(async (_req, res) => ok(res, await svc.softRestaurantSchema())));
router.get('/catalogs', asyncRoute(async (_req, res) => ok(res, await svc.restCardCatalogs())));
router.get('/groups', asyncRoute(async (_req, res) => ok(res, await svc.listRestCardGroups())));
router.post('/groups', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.saveRestCardGroup(req.body || {}, actor(req)))));
router.get('/delivery', asyncRoute(async (req, res) => ok(res, await svc.listRestCardDelivery(req.query))));
router.post('/delivery', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.deliverRestCardCards(req.body || {}, actor(req)))));
router.get('/titulars', asyncRoute(async (req, res) => ok(res, await svc.listRestCardTitulars(req.query))));
router.post('/titulars/assign', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.assignCardToTitular(req.body || {}, actor(req)))));
router.get('/soft-products', asyncRoute(async (req, res) => ok(res, await svc.listSoftProducts(req.query))));
router.get('/courtesies', asyncRoute(async (req, res) => ok(res, await svc.listSoftCourtesies(req.query))));
router.post('/courtesies', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.saveSoftCourtesy(req.body || {}, actor(req)))));
router.post('/courtesies/:id/activate-cards', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.activateCourtesyCards({ ...(req.body || {}), idcortesia: req.params.id }, actor(req)))));
router.post('/generator/preview', asyncRoute(async (req, res) => ok(res, await svc.previewCardNumbers(req.body || {}))));
router.post('/generator/create', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.createCards(req.body || {}, actor(req)))));
router.post('/qa/seed', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.seedQaData(req.body || {}, actor(req)))));
router.get('/cards', asyncRoute(async (req, res) => ok(res, await svc.listCards(req.query))));
router.get('/cards/:number', asyncRoute(async (req, res) => ok(res, await svc.getCard(req.params.number))));
router.put('/cards/:number/assignment', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.updateCardAssignment(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/assignment', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.updateCardAssignment(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/sync-softrestaurant', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.syncSoftRestaurantCard(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/activate', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.manualActivateCard(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/suspend', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.manualSuspendCard(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/cancel', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.cancelCard(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/expiration', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.updateCardExpiration(req.params.number, req.body || {}, actor(req)))));
router.post('/cards/:number/operation', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.cardOperation(req.params.number, req.body || {}, actor(req)))));
router.get('/rules', asyncRoute(async (_req, res) => ok(res, await svc.listRules())));
router.post('/rules', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.saveRule(req.body || {}, actor(req)))));
router.put('/rules/:id', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.saveRule({ ...(req.body || {}), rule_id: req.params.id }, actor(req)))));
router.delete('/rules/:id', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.deleteRule(req.params.id, actor(req)))));
router.post('/rules/:id/evaluate', asyncRoute(async (req, res) => ok(res, await svc.evaluateLimits({ ...(req.body || {}), rule_id: req.params.id }))));
router.post('/limits/evaluate', asyncRoute(async (req, res) => ok(res, await svc.evaluateLimits(req.body || {}))));
router.post('/limits/apply', requireWriter, asyncRoute(async (req, res) => ok(res, await svc.applyLimits({ ...(req.body || {}), actor: actor(req) }))));
router.get('/usage', asyncRoute(async (req, res) => ok(res, await svc.usage(req.query))));
router.get('/swipes', asyncRoute(async (req, res) => ok(res, await svc.recentSwipes(req.query))));
router.get('/audit', asyncRoute(async (req, res) => ok(res, await svc.auditLog(req.query))));
router.get('/sync/preview', asyncRoute(async (_req, res) => ok(res, await svc.syncPreview())));

module.exports = router;
