// BEIE API Routes

import { Router } from 'express';
import { beieMetricsService } from './beie-metrics.service';
import { beieIncentiveService } from './beie-incentive.service';
import { beiePenaltyService } from './beie-penalty.service';
import { beieProfileService } from './beie-profile.service';

const router = Router();

/**
 * @swagger
 * /api/v1/beie/metric:
 *   post:
 *     summary: Calculate behavioral metric
 */
router.post('/metric', async (req, res, next) => {
  try {
    const result = await beieMetricsService.calculateMetric(req.body);
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/metrics/:entityId:
 *   get:
 *     summary: Get all metrics for entity
 */
router.get('/metrics/:entityId', async (req, res, next) => {
  try {
    const metrics = await beieMetricsService.getMetrics(req.params.entityId);
    return res.json(metrics);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/incentive:
 *   post:
 *     summary: Create behavioral incentive
 */
router.post('/incentive', async (req, res, next) => {
  try {
    const result = await beieIncentiveService.createIncentive(req.body);
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/incentive/:incentiveId:
 *   get:
 *     summary: Get incentive
 */
router.get('/incentive/:incentiveId', async (req, res, next) => {
  try {
    const incentive = await beieIncentiveService.getIncentive(req.params.incentiveId);
    if (!incentive) {
      return res.status(404).json({ error: 'Incentive not found' });
    }
    return res.json(incentive);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/incentives/:entityId:
 *   get:
 *     summary: List incentives for entity
 */
router.get('/incentives/:entityId', async (req, res, next) => {
  try {
    const { status } = req.query;
    const incentives = await beieIncentiveService.listIncentives(
      req.params.entityId,
      status as string | undefined
    );
    return res.json(incentives);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/penalty:
 *   post:
 *     summary: Create behavioral penalty
 */
router.post('/penalty', async (req, res, next) => {
  try {
    const result = await beiePenaltyService.createPenalty(req.body);
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/penalty/check/:entityId:
 *   post:
 *     summary: Check and auto-apply penalties
 */
router.post('/penalty/check/:entityId', async (req, res, next) => {
  try {
    const { entityType } = req.body;
    await beiePenaltyService.checkAndApplyPenalties(req.params.entityId, entityType);
    return res.json({ status: 'checked' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/penalties/:entityId:
 *   get:
 *     summary: List penalties for entity
 */
router.get('/penalties/:entityId', async (req, res, next) => {
  try {
    const { status } = req.query;
    const penalties = await beiePenaltyService.listPenalties(
      req.params.entityId,
      status as string | undefined
    );
    return res.json(penalties);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/profile:
 *   post:
 *     summary: Create or update behavioral profile
 */
router.post('/profile', async (req, res, next) => {
  try {
    const result = await beieProfileService.createOrUpdateProfile(req.body);
    return res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/profile/:entityId:
 *   get:
 *     summary: Get behavioral profile
 */
router.get('/profile/:entityId', async (req, res, next) => {
  try {
    const { entityType } = req.query;
    if (!entityType) {
      return res.status(400).json({ error: 'entityType is required' });
    }
    const profile = await beieProfileService.getProfile(
      req.params.entityId,
      entityType as string
    );
    if (!profile) {
      return res.status(404).json({ error: 'Profile not found' });
    }
    return res.json(profile);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/beie/profiles:
 *   get:
 *     summary: List profiles by risk level
 */
router.get('/profiles', async (req, res, next) => {
  try {
    const { riskLevel } = req.query;
    const profiles = await beieProfileService.listProfiles(riskLevel as string | undefined);
    return res.json(profiles);
  } catch (error) {
      return next(error);
  }
});

export default router;

