/**
 * Portfolio Routes
 * Portfolio management and goal tracking endpoints
 */

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

const router = Router();

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

// ============================================================================
// Portfolio Management (Authenticated)
// ============================================================================

/**
 * POST /api/v1/portfolio
 * Create a new portfolio
 * Body: { name, riskProfile, initialValue? }
 */
router.post('/', authHandler(portfolioController.createPortfolio));

/**
 * GET /api/v1/portfolio
 * Get user's portfolios
 */
router.get('/', authHandler(portfolioController.getPortfolios));

/**
 * GET /api/v1/portfolio/:portfolioId
 * Get portfolio by ID with allocations
 */
router.get('/:portfolioId', authHandler(portfolioController.getPortfolio));

/**
 * PUT /api/v1/portfolio/:portfolioId/allocations
 * Update portfolio allocations
 * Body: { allocations: [{ asset, targetPercent }] }
 */
router.put('/:portfolioId/allocations', authHandler(portfolioController.updateAllocations));

/**
 * GET /api/v1/portfolio/:portfolioId/stats
 * Get portfolio statistics
 */
router.get('/:portfolioId/stats', authHandler(portfolioController.getPortfolioStats));

// ============================================================================
// Rebalancing (Authenticated)
// ============================================================================

/**
 * GET /api/v1/portfolio/:portfolioId/rebalance
 * Get rebalancing recommendations
 */
router.get('/:portfolioId/rebalance', authHandler(portfolioController.getRebalanceRecommendations));

/**
 * POST /api/v1/portfolio/:portfolioId/rebalance
 * Execute rebalancing
 */
router.post('/:portfolioId/rebalance', authHandler(portfolioController.executeRebalance));

// ============================================================================
// Goals (Authenticated)
// ============================================================================

/**
 * POST /api/v1/portfolio/goals
 * Create a financial goal
 * Body: { name, targetAmount, targetDate, monthlyContribution }
 */
router.post('/goals', authHandler(portfolioController.createGoal));

/**
 * GET /api/v1/portfolio/goals
 * Get user's goals
 */
router.get('/goals', authHandler(portfolioController.getGoals));

/**
 * PATCH /api/v1/portfolio/goals/:goalId
 * Update goal progress
 * Body: { currentAmount }
 */
router.patch('/goals/:goalId', authHandler(portfolioController.updateGoalProgress));

/**
 * DELETE /api/v1/portfolio/goals/:goalId
 * Delete a goal
 */
router.delete('/goals/:goalId', authHandler(portfolioController.deleteGoal));

export { router as portfolioRouter };
