You are an expert full-stack developer. Your task is to build a fully functional Minimum Viable Product (MVP) for a web application named "Loan Wisdom" (Kredi Akıl). The application will help users manage their loans, specifically focusing on ensuring extra payments reduce the principal effectively and providing transparency.
The application must be built using Next.js 14+ with the App Router, Drizzle ORM for database interactions, and a PostgreSQL database. Tailwind CSS should be used for styling. The application must have a multi-page structure, not a single-page application. All core CRUD functionalities must be implemented and fully working.
Here's the detailed plan:
**1. Project Setup:**
* Initialize a Next.js 14+ project with TypeScript.
* Configure Drizzle ORM with PostgreSQL.
* Set up Tailwind CSS.
**2. Database Schema (Drizzle ORM):**
* **User Table:**
* `id`: Primary Key, UUID
* `email`: String, Unique
* `password_hash`: String
* `created_at`: Timestamp, Default Now
* `updated_at`: Timestamp, Default Now
* **Loan Table:**
* `id`: Primary Key, UUID
* `user_id`: Foreign Key to User.id
* `loan_name`: String (e.g., "Car Loan", "Mortgage")
* `principal_amount`: Numeric (Total initial loan amount)
* `interest_rate`: Numeric (Annual interest rate, e.g., 0.05 for 5%)
* `loan_term_months`: Integer (Original loan term in months)
* `start_date`: Date
* `monthly_minimum_payment`: Numeric
* `current_principal_balance`: Numeric (Calculated, but stored for quick access)
* `created_at`: Timestamp, Default Now
* `updated_at`: Timestamp, Default Now
* **Payment Table:**
* `id`: Primary Key, UUID
* `loan_id`: Foreign Key to Loan.id
* `user_id`: Foreign Key to User.id
* `payment_date`: Date
* `amount_paid`: Numeric
* `is_extra_payment`: Boolean (True if it's an amount above the minimum)
* `applied_to_principal`: Numeric (Amount specifically applied to principal, if known or intended)
* `applied_to_interest`: Numeric (Amount applied to interest, if known)
* `notes`: Text (Optional notes about the payment)
* `created_at`: Timestamp, Default Now
**3. Core Pages (App Router):**
* **`/` (Landing/Home Page):**
* A simple page inviting users to sign up/log in.
* Should not be the primary functional page.
* **`/dashboard`:**
* Protected route (requires authentication).
* Displays a summary of all user's loans.
* Each loan should show: Name, Current Balance, Estimated Payoff Date (based on user's entered payments), and a button to view loan details.
* Button to "Add New Loan".
* **`/loans/new`:**
* Protected route.
* Form to add a new loan (Loan Name, Principal Amount, Interest Rate, Loan Term, Start Date, Monthly Minimum Payment).
* Upon submission, calculate `current_principal_balance` initially as `principal_amount`.
* **`/loans/[id]` (Loan Detail Page):**
* Protected route.
* Displays detailed information for a specific loan:
* All loan details (from Loan Table).
* A list of all payments made for this loan.
* Calculated metrics: Total principal paid, total interest paid, estimated remaining payments, projected payoff date.
* A section for "Payment Simulation": Allow users to input an "extra payment amount" and see how it affects the payoff date and total interest if applied 100% to principal vs. applied to future payments (simple calculation based on loan amortization).
* Button to "Add New Payment".
* **`/loans/[id]/payments/new`:**
* Protected route.
* Form to add a new payment for the specific loan: Payment Date, Amount Paid, Is Extra Payment (checkbox), (Optional) Intended Principal Application Amount.
* Upon submission, update the `current_principal_balance` in the Loan table by subtracting the `applied_to_principal` amount (if provided, otherwise assume minimum payment first applies to interest then principal). This logic should be robust.
* **`/account/profile`:**
* Protected route.
* Basic user profile information (e.g., email).
**4. API Routes (Next.js API Routes within `app/api`):**
* `GET /api/loans`: Get all loans for the authenticated user.
* `POST /api/loans`: Create a new loan.
* `GET /api/loans/[id]`: Get details for a specific loan.
* `PUT /api/loans/[id]`: Update a specific loan. (e.g., adjusting current_principal_balance after an external update)
* `DELETE /api/loans/[id]`: Delete a specific loan.
* `GET /api/loans/[id]/payments`: Get all payments for a specific loan.
* `POST /api/loans/[id]/payments`: Add a new payment for a specific loan.
* `PUT /api/payments/[id]`: Update a specific payment.
* `DELETE /api/payments/[id]`: Delete a specific payment.
* `POST /api/auth/register`: User registration.
* `POST /api/auth/login`: User login (implement a simple session management or JWT for authentication).
**5. Authentication:**
* Implement a basic username/password authentication system.
* Use `bcrypt` for password hashing.
* Protect sensitive routes (`/dashboard`, `/loans/*`, `/account/*`).
**6. Key Logic & Calculations:**
* **Loan Amortization Calculation:** When adding a loan or simulating payments, use standard loan amortization formulas to calculate how much of each payment goes to principal and interest.
* **Principal Reduction Logic:** The system must prioritize applying extra payments directly to the principal *first*, simulating the ideal user behavior. When a payment is recorded, the `current_principal_balance` of the `Loan` should be updated correctl