// SDIP API Routes

import { Router } from 'express';
import { sdipService } from './sdip.service';

const router = Router();

/**
 * @swagger
 * /api/v1/sdip/issue:
 *   post:
 *     summary: Issue a new SDIP passport
 *     tags: [SDIP]
 */
router.post('/issue', async (req, res, next) => {
  try {
    const passport = await sdipService.issuePassport(req.body);
    res.json(passport);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/verify/:passportId:
 *   get:
 *     summary: Verify SDIP passport
 *     tags: [SDIP]
 */
router.get('/verify/:passportId', async (req, res, next) => {
  try {
    const result = await sdipService.verifyPassport(req.params.passportId);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/:passportId:
 *   get:
 *     summary: Get SDIP passport
 *     tags: [SDIP]
 */
router.get('/:passportId', async (req, res, next) => {
  try {
    const passport = await sdipService.getPassport(req.params.passportId);
    if (!passport) {
      return res.status(404).json({ error: 'Passport not found' });
    }
    res.json(passport);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/entity/:entityId:
 *   get:
 *     summary: Get passports by entity ID
 *     tags: [SDIP]
 */
router.get('/entity/:entityId', async (req, res, next) => {
  try {
    const passports = await sdipService.getPassportsByEntity(req.params.entityId);
    res.json(passports);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/:passportId/trust-score:
 *   get:
 *     summary: Calculate trust score for passport
 *     tags: [SDIP]
 */
router.get('/:passportId/trust-score', async (req, res, next) => {
  try {
    const score = await sdipService.calculateTrustScore(req.params.passportId);
    res.json({ passportId: req.params.passportId, trustScore: score });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/:passportId/renew:
 *   post:
 *     summary: Renew SDIP passport
 *     tags: [SDIP]
 */
router.post('/:passportId/renew', async (req, res, next) => {
  try {
    const passport = await sdipService.renewPassport(
      req.params.passportId,
      req.body.validityYears
    );
    res.json(passport);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/:passportId/revoke:
 *   post:
 *     summary: Revoke SDIP passport
 *     tags: [SDIP]
 */
router.post('/:passportId/revoke', async (req, res, next) => {
  try {
    await sdipService.revokePassport({
      passportId: req.params.passportId,
      reason: req.body.reason,
      revokedBy: req.body.revokedBy,
    });
    res.json({ message: 'Passport revoked successfully' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/sdip/expiring:
 *   get:
 *     summary: Get expiring passports
 *     tags: [SDIP]
 */
router.get('/expiring', async (req, res, next) => {
  try {
    const daysAhead = req.query.daysAhead ? parseInt(req.query.daysAhead as string) : 30;
    const passports = await sdipService.getExpiringPassports(daysAhead);
    res.json(passports);
  } catch (error) {
      return next(error);
  }
});

export default router;

