/**
 * @swagger
 * tags:
 *   name: Accounts
 *   description: Bank Account Management
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { accountService } from './account.service';

const router = Router();

/**
 * @swagger
 * /api/accounts:
 *   post:
 *     summary: Create a new bank account
 *     description: Create a new account for a sovereign bank
 *     tags: [Accounts]
 *     security:
 *       - SovereignToken: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - accountType
 *               - currencyCode
 *             properties:
 *               accountType:
 *                 type: string
 *                 enum: [sovereign, treasury, commercial, correspondent, settlement]
 *               currencyCode:
 *                 type: string
 *                 description: ISO 4217 currency code
 *                 example: "USD"
 *               assetType:
 *                 type: string
 *                 enum: [fiat, cbdc, commodity, security]
 *                 default: fiat
 *               reserveRequirement:
 *                 type: string
 *                 description: Reserve requirement percentage
 *     responses:
 *       201:
 *         description: Account created successfully
 *       400:
 *         description: Validation error
 */
router.post('/', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const sovereignBankId = (req as any).sovereignBankId;
    const account = await accountService.createAccount(
      sovereignBankId,
      req.body.accountType,
      req.body.currencyCode,
      req.body.assetType,
      req.body.reserveRequirement
    );

    res.status(201).json({
      success: true,
      data: account,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

/**
 * @swagger
 * /api/accounts/{id}:
 *   get:
 *     summary: Get account by ID
 *     description: Retrieve account details
 *     tags: [Accounts]
 *     security:
 *       - SovereignToken: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Account retrieved
 *       404:
 *         description: Account not found
 */
router.get('/:id', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const account = await accountService.getAccount(req.params.id);
    if (!account) {
      return res.status(404).json({
        success: false,
        error: { code: 'NOT_FOUND', message: 'Account not found' },
        timestamp: new Date(),
      });
    }

    res.json({
      success: true,
      data: account,
      timestamp: new Date(),
    });
  } catch (error) {
    return next(error);
  }
});

export default router;

