You are tasked with building a full-stack web application for a Social Security Benefit Calculator MVP. The application must use Next.js 14 with the App Router, TypeScript, Drizzle ORM for database interactions (PostgreSQL), Tailwind CSS for styling, and Shadcn UI for components. Implement full CRUD functionality where specified. Do not create a single page application or just a landing page. Build a multi-page application with the specified routes and functionality.
**1. Database Schema (Drizzle ORM for PostgreSQL):**
* `users` table:
* `id`: `serial('id').primaryKey()`
* `email`: `varchar('email', { length: 255 }).notNull().unique()`
* `password_hash`: `varchar('password_hash', { length: 255 }).notNull()`
* `created_at`: `timestamp('created_at').defaultNow().notNull()`
* `updated_at`: `timestamp('updated_at').defaultNow().$onUpdate(() => new Date()).notNull()`
* `profiles` table:
* `id`: `serial('id').primaryKey()`
* `userId`: `integer('user_id').notNull().references(() => users.id, { onDelete: 'cascade' }).unique()`
* `first_name`: `varchar('first_name', { length: 100 })`
* `last_name`: `varchar('last_name', { length: 100 })`
* `date_of_birth`: `date('date_of_birth')`
* `retirement_age_target`: `integer('retirement_age_target').default(67)`
* `earnings` table:
* `id`: `serial('id').primaryKey()`
* `userId`: `integer('user_id').notNull().references(() => users.id, { onDelete: 'cascade' })`
* `year`: `integer('year').notNull()`
* `taxable_earnings`: `numeric('taxable_earnings', { precision: 10, scale: 2 }).notNull()`
* `created_at`: `timestamp('created_at').defaultNow().notNull()`
* `updated_at`: `timestamp('updated_at').defaultNow().$onUpdate(() => new Date()).notNull()`
**2. Pages (App Router `app/` directory):**
* `/`: Home page with a clear value proposition, features overview, and call-to-action buttons (Login/Register).
* `/register`: User registration form (email, password, confirm password).
* `/login`: User login form (email, password).
* `/dashboard`: Authenticated user's main dashboard. Display a summary of user's profile and a list of their entered earnings. Include buttons for 'Add New Earning Year', 'View Calculation', and 'Edit Profile'.
* `/earnings/add`: Form to add a new annual earning entry (year, taxable_earnings). This should be a client component.
* `/earnings/[id]/edit`: Form to edit an existing annual earning entry. Pre-fill with existing data. This should be a client component.
* `/calculation`: Page to display the estimated Social Security benefit. Allow users to adjust their target retirement age (e.g., 62, 67, 70) and re-calculate. This page will fetch user earnings data and perform the calculation. Display the AIME and PIA components of the calculation.
* `/profile`: User profile management page. Display and allow editing of `first_name`, `last_name`, `date_of_birth`, and `retirement_age_target`. This should be a client component.
**3. API Routes (`app/api` directory):**
* `/api/auth/register` (POST): Handles user registration, hashes password, creates a user, and a default profile. Returns a JWT.
* `/api/auth/login` (POST): Handles user login, verifies credentials, and returns a JWT.
* `/api/user/profile` (GET, PUT):
* GET: Fetches the authenticated user's profile data.
* PUT: Updates the authenticated user's profile data.
* `/api/earnings` (GET, POST):
* GET: Fetches all earning entries for the authenticated user.
* POST: Creates a new earning entry for the authenticated user.
* `/api/earnings/[id]` (GET, PUT, DELETE):
* GET: Fetches a single earning entry by ID for the authenticated user.
* PUT: Updates an existing earning entry by ID for the authenticated user.
* DELETE: Deletes an earning entry by ID for the authenticated user.
* `/api/calculate-ss` (POST): Receives user earnings and profile data, performs the Social Security benefit calculation (AIME, PIA) based on SSA rules (using placeholder bend points and indexing factors for current year for MVP), and returns the estimated monthly benefit. This calculation logic should be robust.
**4. Authentication:**
* Implement simple JWT-based authentication. The JWT should be stored in HTTP-only cookies on successful login/registration. Protect all authenticated routes and API endpoints.
* A middleware (`middleware.ts`) should check for authentication on protected routes.
**5. Social Security Calculation Logic (for `/api/calculate-ss`):**
* The API should implement the core logic of SSA's benefit calculation:
* **Wage Indexing**: Index historical earnings to current wage levels. Use simplified indexing factors (e.g., assume a fixed average index for years prior to the current year if exact historical data is too complex for MVP, or hardcode a few recent years' factors).
* **Average Indexed Monthly Earnings (AIME)**: Select the 35 highest indexed earning years. If less than 35 years of earnings are provided, include zero-earnings years for the remaining count. Sum these 35 years and divide by 420 (35 years * 12 months).
* **Primary Insurance Amount (PIA)**: Apply the SSA's bend points to the AIME. For the MVP, use current year's bend points (e.g., 90% of the first X, 32% of the amount between X and Y, 15% of the amount over Y). Hardcode these values.
* **Early/Late Retirement Adjustments**: Factor in adjustments for retiring before or after Full Retirement Age (FRA). Assume FRA is 67 for now, and apply standard reduction/increase percentages for early/late retirement.
* All calculation steps should be clearly documented within the code.
**6. UI/UX:**
* Use Tailwind CSS and Shadcn UI components for a clean, modern, and responsive interface.
* Provide clear feedback to users for form submissions, errors, and loading states.
**7. Deployment:**
* Structure the project to be easily deploy