You are tasked with developing a full-stack MVP for a 'Vehicle Finance Compare' application using Next.js 14 with the App Router, TypeScript, and Tailwind CSS. The application should enable users to compare new vs. used car purchase scenarios, factoring in financing APRs and alternative investment opportunities. Implement a multi-page architecture with full CRUD functionality for user-saved scenarios.
**Database and ORM:**
* Utilize PostgreSQL for the database (or SQLite for local development, with clear instructions for switching).
* Use Drizzle ORM for database interactions and schema definition.
* Define the following Drizzle schema:
* `users` table: `id` (serial primary key), `email` (unique text), `passwordHash` (text), `createdAt` (timestamp with default now), `updatedAt` (timestamp).
* `scenarios` table: `id` (serial primary key), `userId` (integer foreign key to users.id), `scenarioName` (text, e.g., 'My New Car vs Used Option'), `newCarPrice` (numeric), `newCarDownPayment` (numeric), `newCarApr` (numeric), `newCarLoanTermMonths` (integer), `usedCarPrice` (numeric), `usedCarDownPayment` (numeric), `usedCarApr` (numeric), `usedCarLoanTermMonths` (integer), `cashInvestmentAmount` (numeric), `expectedInvestmentReturnRate` (numeric), `createdAt` (timestamp with default now), `updatedAt` (timestamp).
**API Routes (app/api/):**
* Implement API routes for full CRUD operations on `scenarios`:
* `GET /api/scenarios`: Fetch all scenarios for the authenticated user.
* `POST /api/scenarios`: Create a new scenario. Requires `userId`.
* `GET /api/scenarios/[id]`: Fetch a specific scenario by ID for the authenticated user.
* `PUT /api/scenarios/[id]`: Update an existing scenario by ID for the authenticated user.
* `DELETE /api/scenarios/[id]`: Delete a scenario by ID for the authenticated user.
* Implement basic user authentication API routes:
* `POST /api/auth/register`: Register a new user.
* `POST /api/auth/login`: Authenticate an existing user and return a session token/cookie.
**Frontend (Next.js App Router):**
* **`app/layout.tsx`**: Basic layout with a navigation bar.
* **`app/page.tsx`**: Homepage/Dashboard. Display a list of the user's saved scenarios. Provide a clear call to action to create a new scenario. Show a summary of each scenario (e.g., name, net gain/loss).
* **`app/auth/register/page.tsx`**: User registration form.
* **`app/auth/login/page.tsx`**: User login form.
* **`app/scenarios/new/page.tsx`**: Form for creating a new car comparison scenario.
* Input fields for: New Car Price, New Car Down Payment, New Car APR (%), New Car Loan Term (months).
* Input fields for: Used Car Price, Used Car Down Payment, Used Car APR (%), Used Car Loan Term (months).
* Input fields for: Cash Investment Amount (the difference between used car cash price and new car down payment), Expected Annual Investment Return Rate (%).
* A 'Calculate' button to preview results without saving.
* A 'Save Scenario' button to save the scenario to the database.
* **`app/scenarios/[id]/page.tsx`**: Detailed comparison report page for a specific scenario.
* Display all input parameters for both new and used car scenarios, and investment details.
* Show calculated results: Monthly payments for both car options, total interest paid, total cost over loan term, estimated investment growth, and the final net difference (arbitrage gain/loss).
* Include a button to 'Edit Scenario' (navigates to an edit form) and 'Delete Scenario'.
* **`app/scenarios/[id]/edit/page.tsx`**: Form for editing an existing scenario. Pre-populate fields with existing scenario data.
**Core Logic (Server-side utilities):**
* Implement a server-side utility function to calculate monthly loan payments (PMT formula).
* Implement a server-side utility function to calculate total interest paid and total cost.
* Implement a server-side utility function to estimate future value of an investment (compound interest).
**Styling:**
* Use Tailwind CSS for all styling. Ensure a clean, responsive, and intuitive user interface.
**Additional Notes:**
* Focus on robust data validation for all inputs.
* Implement basic error handling and loading states for API interactions.
* Ensure secure password handling (hashing).
* The application should not be a single page application or just a landing page. It must have distinct pages as outlined.