/**
 * @file tokenization.routes.ts
 * @notice REST API routes for tokenization capability
 */

import { Router } from 'express';
import { tokenizationService } from './tokenization.service';
import { EntitlementCheckContext } from '@/shared/solacenet/types';

const router = Router();

/**
 * @swagger
 * /api/v1/solacenet/tokenization/mint:
 *   post:
 *     tags: [SolaceNet Tokenization]
 *     summary: Mint tokenized asset
 *     description: Mint a new tokenized asset (requires tokenization.mint capability)
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - underlyingAsset
 *               - amount
 *               - issuer
 *               - reserveId
 *             properties:
 *               underlyingAsset:
 *                 type: string
 *                 example: EUR
 *               amount:
 *                 type: string
 *                 example: "1000000.00"
 *               issuer:
 *                 type: string
 *                 example: "0x..."
 *               reserveId:
 *                 type: string
 *                 example: "RESERVE-EUR-001"
 *               recipient:
 *                 type: string
 *               regulatoryFlags:
 *                 type: object
 *     responses:
 *       200:
 *         description: Tokenization initiated successfully
 *       403:
 *         description: Capability not granted
 */
router.post('/mint', async (req, res) => {
    try {
        const context: EntitlementCheckContext = {
            tenantId: req.headers['x-tenant-id'] as string || 'default',
            programId: req.headers['x-program-id'] as string || 'default',
            region: req.headers['x-region'] as string || 'default',
            channel: req.headers['x-channel'] as string || 'default',
            capabilityId: 'tokenization.mint',
        };

        const result = await tokenizationService.mintToken(req.body, context);
        res.json(result);
    } catch (error: any) {
        if (error.message.includes('capability')) {
            res.status(403).json({ error: error.message });
        } else {
            res.status(500).json({ error: error.message });
        }
    }
});

/**
 * @swagger
 * /api/v1/solacenet/tokenization/transfer:
 *   post:
 *     tags: [SolaceNet Tokenization]
 *     summary: Transfer tokenized asset
 *     description: Transfer tokenized asset (requires tokenization.transfer capability)
 */
router.post('/transfer', async (req, res) => {
    try {
        const context: EntitlementCheckContext = {
            tenantId: req.headers['x-tenant-id'] as string || 'default',
            programId: req.headers['x-program-id'] as string || 'default',
            region: req.headers['x-region'] as string || 'default',
            channel: req.headers['x-channel'] as string || 'default',
            capabilityId: 'tokenization.transfer',
        };

        const { tokenId, from, to, amount } = req.body;
        const result = await tokenizationService.transferToken(tokenId, from, to, amount, context);
        res.json(result);
    } catch (error: any) {
        if (error.message.includes('capability')) {
            res.status(403).json({ error: error.message });
        } else {
            res.status(500).json({ error: error.message });
        }
    }
});

/**
 * @swagger
 * /api/v1/solacenet/tokenization/redeem:
 *   post:
 *     tags: [SolaceNet Tokenization]
 *     summary: Redeem tokenized asset
 *     description: Redeem tokenized asset back to underlying asset (requires tokenization.redeem capability)
 */
router.post('/redeem', async (req, res) => {
    try {
        const context: EntitlementCheckContext = {
            tenantId: req.headers['x-tenant-id'] as string || 'default',
            programId: req.headers['x-program-id'] as string || 'default',
            region: req.headers['x-region'] as string || 'default',
            channel: req.headers['x-channel'] as string || 'default',
            capabilityId: 'tokenization.redeem',
        };

        const { tokenId, redeemer, amount } = req.body;
        const result = await tokenizationService.redeemToken(tokenId, redeemer, amount, context);
        res.json(result);
    } catch (error: any) {
        if (error.message.includes('capability')) {
            res.status(403).json({ error: error.message });
        } else {
            res.status(500).json({ error: error.message });
        }
    }
});

/**
 * @swagger
 * /api/v1/solacenet/tokenization/status/:requestId:
 *   get:
 *     tags: [SolaceNet Tokenization]
 *     summary: Get tokenization status
 *     description: Get status of tokenization request (requires tokenization.view capability)
 */
router.get('/status/:requestId', async (req, res) => {
    try {
        const context: EntitlementCheckContext = {
            tenantId: req.headers['x-tenant-id'] as string || 'default',
            programId: req.headers['x-program-id'] as string || 'default',
            region: req.headers['x-region'] as string || 'default',
            channel: req.headers['x-channel'] as string || 'default',
            capabilityId: 'tokenization.view',
        };

        const result = await tokenizationService.getStatus(req.params.requestId, context);
        res.json(result);
    } catch (error: any) {
        if (error.message.includes('capability')) {
            res.status(403).json({ error: error.message });
        } else {
            res.status(500).json({ error: error.message });
        }
    }
});

/**
 * @swagger
 * /api/v1/solacenet/tokenization/token/:tokenId:
 *   get:
 *     tags: [SolaceNet Tokenization]
 *     summary: Get tokenized asset details
 *     description: Get details of a tokenized asset (requires tokenization.view capability)
 */
router.get('/token/:tokenId', async (req, res) => {
    try {
        const context: EntitlementCheckContext = {
            tenantId: req.headers['x-tenant-id'] as string || 'default',
            programId: req.headers['x-program-id'] as string || 'default',
            region: req.headers['x-region'] as string || 'default',
            channel: req.headers['x-channel'] as string || 'default',
            capabilityId: 'tokenization.view',
        };

        const result = await tokenizationService.getTokenDetails(req.params.tokenId, context);
        res.json(result);
    } catch (error: any) {
        if (error.message.includes('capability')) {
            res.status(403).json({ error: error.message });
        } else {
            res.status(500).json({ error: error.message });
        }
    }
});

export default router;
