import { Router, type IRouter } from 'express';
import * as satController from '../controllers/sat.controller.js';
import { authenticate, authorize } from '../middlewares/auth.middleware.js';
import { tenantMiddleware } from '../middlewares/tenant.middleware.js';
import { veryStrictLimit } from '../middlewares/rate-limit.middleware.js';

const router: IRouter = Router();

router.use(authenticate);
router.use(tenantMiddleware);

// POST /api/sat/sync - Iniciar sincronización manual (2/día por user, admin global exento)
router.post('/sync', veryStrictLimit, satController.start);

// GET /api/sat/sync/status - Estado actual de sincronización
router.get('/sync/status', satController.status);

// GET /api/sat/sync/history - Historial de sincronizaciones
router.get('/sync/history', satController.history);

// GET /api/sat/sync/:id - Detalle de un job
router.get('/sync/:id', satController.jobDetail);

// POST /api/sat/sync/:id/retry - Reintentar job fallido
router.post('/sync/:id/retry', satController.retry);

// Admin-only cron endpoints (global admin verified in controller)
router.get('/cron', authorize('owner', 'cfo'), satController.cronInfo);
router.post('/cron/run', authorize('owner', 'cfo'), satController.runCron);

export default router;
