/**
 * @swagger
 * tags:
 *   name: SolaceNet Wallets
 *   description: Wallet accounts and transfers
 */

import { Router } from 'express';
import { zeroTrustAuthMiddleware } from '@/integration/api-gateway/middleware/auth.middleware';
import { walletAccountsService, CreateWalletRequest, WalletTransferRequest } from './wallet-accounts.service';

const router = Router();

router.post('/', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as CreateWalletRequest;
    const wallet = await walletAccountsService.createWallet(request);
    res.status(201).json(wallet);
  } catch (error) {
    next(error);
  }
});

router.get('/:walletId', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const { walletId } = req.params;
    const tenantId = req.headers['x-tenant-id'] as string;
    const wallet = await walletAccountsService.getWallet(walletId, tenantId);
    if (!wallet) {
      return res.status(404).json({ error: 'Wallet not found' });
    }
    res.json(wallet);
  } catch (error) {
    next(error);
  }
});

router.post('/transfer', zeroTrustAuthMiddleware, async (req, res, next) => {
  try {
    const request = req.body as WalletTransferRequest;
    await walletAccountsService.transfer(request);
    res.json({ success: true });
  } catch (error) {
    next(error);
  }
});

export default router;
