/**
 * System Config Routes
 *
 * Express router for system configuration endpoints
 */

import { Router } from 'express';
import { PrismaClient } from '@prisma/client';
import { SystemConfigController } from './system-config.controller';
import { SystemConfigService } from './system-config.service';

const prisma = new PrismaClient();
const service = new SystemConfigService(prisma);
const controller = new SystemConfigController(service);

const router = Router();

// Async handler wrapper
const asyncHandler = (fn: Function) => (req: any, res: any, next: any) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};

// Public routes (no auth required)
router.get('/public', asyncHandler(controller.getPublic.bind(controller)));

// Protected routes (require authentication)
router.get('/by-category/:category', asyncHandler(controller.getByCategory.bind(controller)));
router.get('/by-key/:key', asyncHandler(controller.getByKey.bind(controller)));

// Admin-only routes
router.get('/all', asyncHandler(controller.getAll.bind(controller)));
router.post('/', asyncHandler(controller.upsert.bind(controller)));
router.put('/:key', asyncHandler(controller.updateValue.bind(controller)));
router.delete('/:key', asyncHandler(controller.delete.bind(controller)));
router.get('/:key/history', asyncHandler(controller.getChangeHistory.bind(controller)));

export const systemConfigRoutes = router;
export default router;
