import { Router, Request, Response } from 'express';
import { getPublicConfig } from '../config/system.config';
import { asyncHandler } from '../middlewares/errorHandler.middleware';
import { activityTypeService } from '../services/activityType.service';
import { generalApiLimiter } from '../middlewares/rateLimiter.middleware';
import { authenticateToken } from '../middlewares/auth.middleware';

const router = Router();

// All config routes get general rate limiting (public endpoints)
router.use(generalApiLimiter);

/**
 * GET /api/v1/config
 * Returns public system configuration for frontend
 * No authentication required - only returns non-sensitive values
 */
router.get('/',
  asyncHandler(async (req: Request, res: Response): Promise<void> => {
    const config = await getPublicConfig();
    res.json({
      success: true,
      data: config
    });
    return;
  })
);

/**
 * GET /api/v1/config/activity-types
 * Returns all active activity types for frontend
 * No authentication required - public endpoint
 */
router.get('/activity-types',
  authenticateToken,
  asyncHandler(async (req: Request, res: Response): Promise<void> => {
    const activityTypes = await activityTypeService.getAllActivityTypes(true);
    res.json({
      success: true,
      data: activityTypes.map((at: any) => ({
        activityTypeId: at.activityTypeId,
        title: at.title,
        itemCode: at.itemCode,
        taxationType: at.taxationType,
        sapRefNo: at.sapRefNo,
        creditPostingOn: at.creditPostingOn
      }))
    });
    return;
  })
);
export default router;
