Build a comprehensive Next.js application using the App Router structure. The application should help users make informed decisions about whether to prioritize paying down their mortgage or investing surplus cash, especially during market downturns. The MVP should include full CRUD (Create, Read, Update, Delete) functionality for user profiles, financial goals, and investment/debt data. Use Drizzle ORM with PostgreSQL as the database. Implement robust API routes for all data operations.
**Project Structure:**
- Use the `app/` directory for all routing.
- Create separate directories for core features like `(auth)`, `(dashboard)`, `(investment-planner)`.
- Implement server components and client components where appropriate.
**Authentication:**
- Implement secure user authentication (e.g., using NextAuth.js or Clerk).
- Ensure password hashing and secure session management.
**Database Schema (Drizzle ORM):**
1. `users` table: `id`, `name`, `email`, `passwordHash`, `createdAt`, `updatedAt`.
2. `financialProfiles` table: `id`, `userId` (foreign key to users), `monthlyIncome`, `monthlyExpenses`, `riskTolerance` (e.g., 'low', 'medium', 'high'), `retirementAge`, `createdAt`, `updatedAt`.
3. `debts` table: `id`, `userId` (foreign key), `type` (e.g., 'mortgage', 'car_loan'), `principal`, `interestRate`, `remainingTermMonths`, `startDate`, `createdAt`, `updatedAt`.
4. `investments` table: `id`, `userId` (foreign key), `type` (e.g., 'stock', 'mutual_fund', 'crypto'), `name`, `currentValue`, `purchaseDate`, `initialInvestment`, `createdAt`, `updatedAt`.
5. `financialGoals` table: `id`, `userId` (foreign key), `name`, `targetAmount`, `targetDate`, `currentAmount`, `priority`, `createdAt`, `updatedAt`.
**API Routes:**
- Implement RESTful API routes under `app/api/` for all CRUD operations for each of the tables defined above.
- Example: `app/api/users/`, `app/api/financialProfiles/`, `app/api/debts/`, `app/api/investments/`, `app/api/financialGoals/`.
- Ensure proper request validation and error handling.
**Core Application Logic (Multi-page, not a SPA):**
1. **Onboarding/Profile Setup:** A multi-step form guiding users to create their `financialProfile`, add initial `debts` (especially mortgage details), and potentially their first `investments`.
2. **Dashboard:** A central page (`app/(dashboard)/page.tsx`) displaying a summary of the user's financial health: net worth, debt overview, investment performance, and progress towards `financialGoals`.
3. **Debt Management Module:** A dedicated section (`app/(investment-planner)/debts/page.tsx`) to view, add, edit, and delete debts. This page should include a calculator to simulate the impact of extra mortgage payments vs. investing.
4. **Investment Management Module:** A section (`app/(investment-planner)/investments/page.tsx`) to view, add, edit, and delete investment accounts and their current values. Include basic performance tracking.
5. **Decision Analysis Engine:** The core feature. This engine should take user inputs (income, expenses, debt details, investment values, risk tolerance, market conditions - simulated or user-inputted) and provide actionable recommendations. For MVP, simulate average market returns (e.g., 7-10% for stocks, 3-5% for bonds) and compare them against the user's mortgage interest rate (e.g., 5.25%). Clearly present the pros and cons of paying down the mortgage versus investing based on these inputs.
6. **Goal Planning:** A section (`app/(dashboard)/goals/page.tsx`) to set and track progress towards specific financial goals.
**UI/UX Considerations:**
- Use a clean and intuitive UI design. Consider using a component library like Shadcn/ui.
- Ensure the application is responsive and works well on different devices.
- Provide clear visualizations (charts, graphs) for financial data and projections.
**Key Functionality:**
- Simulate scenarios: "What if I pay an extra $X per month on my mortgage?" vs. "What if I invest an extra $X per month?"
- Display projected timelines for debt freedom and goal achievement under different strategies.
- The AI Master Prompt should not generate a simple landing page or a SPA. It must encompass the full application architecture with database, API routes, and multi-page navigation as described.