/**
 * Investment Routes
 * Products, accounts, and transaction endpoints
 */

import { Router, RequestHandler } from 'express';
import * as investmentController from './controllers/investment.controller';

const router = Router();

// Type cast helper for authenticated routes
const authHandler = (fn: Function): RequestHandler => fn as RequestHandler;

// ============================================================================
// Product Routes (Public)
// ============================================================================

/**
 * GET /api/v1/investment/products
 * Get all investment products
 * Query params: riskProfile
 */
router.get('/products', investmentController.getProducts);

/**
 * GET /api/v1/investment/products/:productId
 * Get product details with stats
 */
router.get('/products/:productId', investmentController.getProductById);

/**
 * GET /api/v1/investment/products/:productId/performance
 * Get product performance history
 * Query params: period (week, month, 3months, year)
 */
router.get('/products/:productId/performance', investmentController.getProductPerformance);

// ============================================================================
// Account Routes (Authenticated)
// TODO: Add authentication middleware
// ============================================================================

/**
 * GET /api/v1/investment/accounts
 * Get user's investment accounts
 */
router.get('/accounts', authHandler(investmentController.getUserAccounts));

/**
 * GET /api/v1/investment/accounts/summary
 * Get account summary (portfolio overview)
 */
router.get('/accounts/summary', authHandler(investmentController.getAccountSummary));

/**
 * POST /api/v1/investment/accounts
 * Create a new investment account
 * Body: { productId, initialDeposit }
 */
router.post('/accounts', authHandler(investmentController.createAccount));

/**
 * GET /api/v1/investment/accounts/:accountId
 * Get account details with performance
 */
router.get('/accounts/:accountId', authHandler(investmentController.getAccountById));

/**
 * POST /api/v1/investment/accounts/:accountId/close
 * Close an investment account
 */
router.post('/accounts/:accountId/close', authHandler(investmentController.closeAccount));

// ============================================================================
// Transaction Routes (Authenticated)
// ============================================================================

/**
 * GET /api/v1/investment/accounts/:accountId/transactions
 * Get account transactions
 * Query params: type, status, limit, offset
 */
router.get('/accounts/:accountId/transactions', authHandler(investmentController.getTransactions));

/**
 * POST /api/v1/investment/accounts/:accountId/deposit
 * Create a deposit
 * Body: { amount }
 */
router.post('/accounts/:accountId/deposit', authHandler(investmentController.createDeposit));

/**
 * POST /api/v1/investment/accounts/:accountId/withdraw
 * Create a withdrawal request
 * Body: { amount, bankInfo?, cryptoInfo? }
 */
router.post('/accounts/:accountId/withdraw', authHandler(investmentController.createWithdrawal));

/**
 * GET /api/v1/investment/accounts/:accountId/distributions
 * Get account distributions
 */
router.get('/accounts/:accountId/distributions', authHandler(investmentController.getDistributions));

// ============================================================================
// Withdrawal Routes (Authenticated)
// ============================================================================

/**
 * GET /api/v1/investment/withdrawals
 * Get user's withdrawal requests
 * Query params: status
 */
router.get('/withdrawals', authHandler(investmentController.getWithdrawals));

export { router as investmentRouter };
