// UDFO API Routes

import { Router } from 'express';
import { udfoService } from './udfo.service';
import { ontologyValidatorService } from './ontology-validator.service';
import { assetOntologyService } from './asset-ontology.service';
import { entityOntologyService } from './entity-ontology.service';
import { processOntologyService } from './process-ontology.service';

const router = Router();

/**
 * @swagger
 * /api/v1/udfo/initialize:
 *   post:
 *     summary: Initialize UDFO with default definitions
 *     tags: [UDFO]
 */
router.post('/initialize', async (req, res, next) => {
  try {
    await udfoService.initialize();
    res.json({ message: 'UDFO initialized successfully' });
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/definitions:
 *   get:
 *     summary: Get all ontology definitions
 *     tags: [UDFO]
 */
router.get('/definitions', async (req, res, next) => {
  try {
    const definitions = await udfoService.getAllDefinitions();
    res.json(definitions);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/assets:
 *   get:
 *     summary: Get all asset definitions
 *     tags: [UDFO]
 */
router.get('/assets', async (req, res, next) => {
  try {
    const assets = await assetOntologyService.getAllAssets();
    res.json(assets);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/assets/:code:
 *   get:
 *     summary: Get asset by code
 *     tags: [UDFO]
 */
router.get('/assets/:code', async (req, res, next) => {
  try {
    const asset = await assetOntologyService.getAssetByCode(req.params.code);
    if (!asset) {
      return res.status(404).json({ error: 'Asset not found' });
    }
    res.json(asset);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/entities:
 *   get:
 *     summary: Get all entity definitions
 *     tags: [UDFO]
 */
router.get('/entities', async (req, res, next) => {
  try {
    const entities = await entityOntologyService.getAllEntities();
    res.json(entities);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/entities/:identifier:
 *   get:
 *     summary: Get entity by identifier
 *     tags: [UDFO]
 */
router.get('/entities/:identifier', async (req, res, next) => {
  try {
    const entity = await entityOntologyService.getEntityByIdentifier(req.params.identifier);
    if (!entity) {
      return res.status(404).json({ error: 'Entity not found' });
    }
    res.json(entity);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/processes:
 *   get:
 *     summary: Get all process definitions
 *     tags: [UDFO]
 */
router.get('/processes', async (req, res, next) => {
  try {
    const processes = await processOntologyService.getAllProcesses();
    res.json(processes);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/processes/:code:
 *   get:
 *     summary: Get process by code
 *     tags: [UDFO]
 */
router.get('/processes/:code', async (req, res, next) => {
  try {
    const process = await processOntologyService.getProcessByCode(req.params.code);
    if (!process) {
      return res.status(404).json({ error: 'Process not found' });
    }
    res.json(process);
  } catch (error) {
      return next(error);
  }
});

/**
 * @swagger
 * /api/v1/udfo/validate:
 *   post:
 *     summary: Validate ontology definitions
 *     tags: [UDFO]
 */
router.post('/validate', async (req, res, next) => {
  try {
    const result = await ontologyValidatorService.validate(req.body);
    res.json(result);
  } catch (error) {
      return next(error);
  }
});

export default router;

