/**
 * @swagger
 * tags:
 *   name: SolaceNet Capability Registry
 *   description: Capability registry management - register, update, and manage capabilities
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { capabilityRegistryService } from './capability-registry.service';
import { CreateCapabilityRequest, UpdateCapabilityRequest } from '@/shared/solacenet/types';

const router = Router();

/**
 * @swagger
 * /api/v1/solacenet/capabilities:
 *   get:
 *     summary: List all capabilities
 *     description: Get all registered capabilities
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     responses:
 *       200:
 *         description: List of capabilities
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Capability'
 */
router.get('/', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const capabilities = await capabilityRegistryService.getAllCapabilities();
    res.json(capabilities);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}:
 *   get:
 *     summary: Get capability by ID
 *     description: Get details of a specific capability
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: Capability ID
 *     responses:
 *       200:
 *         description: Capability details
 *       404:
 *         description: Capability not found
 */
router.get('/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const capability = await capabilityRegistryService.getCapabilityById(req.params.id);
    if (!capability) {
      return res.status(404).json({ error: 'Capability not found' });
    }
    res.json(capability);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities:
 *   post:
 *     summary: Register new capability
 *     description: Register a new capability in the registry
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - capabilityId
 *               - name
 *             properties:
 *               capabilityId:
 *                 type: string
 *               name:
 *                 type: string
 *               version:
 *                 type: string
 *               description:
 *                 type: string
 *               ownerTeam:
 *                 type: string
 *               dependencies:
 *                 type: array
 *                 items:
 *                   type: string
 *               configSchema:
 *                 type: object
 *               defaultState:
 *                 type: string
 *                 enum: [disabled, pilot, enabled, suspended, drain]
 *     responses:
 *       201:
 *         description: Capability created
 *       400:
 *         description: Validation error
 */
router.post('/', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as CreateCapabilityRequest;
    const capability = await capabilityRegistryService.createCapability(request);
    res.status(201).json(capability);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}:
 *   put:
 *     summary: Update capability
 *     description: Update an existing capability
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               version:
 *                 type: string
 *               description:
 *                 type: string
 *               ownerTeam:
 *                 type: string
 *               dependencies:
 *                 type: array
 *                 items:
 *                   type: string
 *               configSchema:
 *                 type: object
 *               defaultState:
 *                 type: string
 *               status:
 *                 type: string
 *                 enum: [active, deprecated, archived]
 *     responses:
 *       200:
 *         description: Capability updated
 *       404:
 *         description: Capability not found
 */
router.put('/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as UpdateCapabilityRequest;
    const capability = await capabilityRegistryService.updateCapability(req.params.id, request);
    res.json(capability);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}:
 *   delete:
 *     summary: Delete capability
 *     description: Archive a capability (soft delete)
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       204:
 *         description: Capability archived
 *       404:
 *         description: Capability not found
 */
router.delete('/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    await capabilityRegistryService.deleteCapability(req.params.id);
    res.status(204).send();
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}/dependencies:
 *   get:
 *     summary: Get capability dependencies
 *     description: Get all dependencies for a capability
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: List of dependencies
 */
router.get('/:id/dependencies', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const dependencies = await capabilityRegistryService.getCapabilityDependencies(req.params.id);
    res.json(dependencies);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}/bindings:
 *   get:
 *     summary: Get capability bindings
 *     description: Get all provider bindings for a capability
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: List of bindings
 */
router.get('/:id/bindings', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const bindings = await capabilityRegistryService.getCapabilityBindings(req.params.id);
    res.json(bindings);
  } catch (error) {
    next(error);
  }
});

/**
 * @swagger
 * /api/v1/solacenet/capabilities/{id}/bindings:
 *   post:
 *     summary: Create capability binding
 *     description: Create a provider binding for a capability in a region
 *     tags: [SolaceNet Capability Registry]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - region
 *             properties:
 *               region:
 *                 type: string
 *               providerId:
 *                 type: string
 *               config:
 *                 type: object
 *     responses:
 *       201:
 *         description: Binding created
 */
router.post('/:id/bindings', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const { region, providerId, config } = req.body;
    const binding = await capabilityRegistryService.createCapabilityBinding(
      req.params.id,
      region,
      providerId,
      config
    );
    res.status(201).json(binding);
  } catch (error) {
    next(error);
  }
});

export default router;
