Develop a full-stack web application named "Scarcity Kitchen" using Next.js 14 with the App Router, TypeScript, Tailwind CSS for styling, Drizzle ORM for database interactions, and PostgreSQL as the database. The application aims to help users create meal plans and grocery lists based on extremely limited budgets, existing pantry items, and available food bank resources. It must include full CRUD functionality for all core entities and a multi-page structure, not just a single-page application or a landing page.
**Database Schema (Drizzle ORM):**
1. **users**: `id` (PK), `email` (unique), `password` (hashed), `username`, `created_at`, `updated_at`.
2. **pantryItems**: `id` (PK), `userId` (FK to users), `name` (item name), `quantity`, `unit`, `expiration_date` (nullable), `is_from_food_bank` (boolean), `created_at`, `updated_at`.
3. **mealPlans**: `id` (PK), `userId` (FK to users), `start_date`, `end_date`, `total_budget` (number, money available for the plan), `created_at`, `updated_at`.
4. **mealPlanRecipes**: `id` (PK), `mealPlanId` (FK to mealPlans), `recipeId` (FK to recipes), `day_of_week` (integer 1-7), `meal_type` (e.g., 'breakfast', 'lunch', 'dinner', 'snack'), `created_at`.
5. **recipes**: `id` (PK), `name`, `description`, `ingredients` (JSONB array of {name, quantity, unit}), `instructions` (text), `estimated_cost` (number), `nutritional_info` (JSONB object), `tags` (JSONB array for 'low-cost', 'high-protein', 'vegan'), `created_at`, `updated_at`.
6. **groceryLists**: `id` (PK), `userId` (FK to users), `mealPlanId` (FK to mealPlans, nullable), `name`, `created_at`, `updated_at`.
7. **groceryListItems**: `id` (PK), `groceryListId` (FK to groceryLists), `name`, `quantity`, `unit`, `is_purchased` (boolean), `created_at`.
8. **localResources**: `id` (PK), `name`, `type` (e.g., 'food_bank', 'low_cost_store', 'shelter'), `address`, `latitude`, `longitude`, `phone`, `website`, `opening_hours` (JSONB), `created_at`, `updated_at`.
**Pages (Next.js App Router Structure - `app/` directory):**
- `/`: Public landing page describing the app's value.
- `/dashboard`: User's authenticated homepage. Displays an overview of current meal plan, budget, and quick links.
- `/pantry`: Dedicated page for managing (`CRUD`) user's `pantryItems`.
- `/budget`: Page to input and manage the user's available `total_budget` for food.
- `/meal-plan`: Main page for `mealPlan` management. Users can generate, view, edit, and delete `mealPlans`. This page should trigger the meal plan generation logic.
- `/recipes`: Browse `recipes`. Initial recipes should be seeded or fetched from a low-cost dataset.
- `/grocery-list`: View and manage (`CRUD` for items) `groceryLists`. Mark items as purchased.
- `/resources`: Displays a list and map view of `localResources` (food banks, low-cost stores) based on user's location. Includes search/filter functionality.
- `/auth/login`, `/auth/register`: User authentication pages.
- `/profile`: User settings and dietary preferences (basic for MVP).
**API Routes (Next.js API routes within `app/api`):**
- `/api/auth/[...nextauth]`: NextAuth.js endpoints for authentication.
- `/api/pantry`: Handles `CRUD` for `pantryItems`.
- `/api/meal-plans`: Handles `CRUD` for `mealPlans`. Includes a dedicated endpoint or function for triggering meal plan generation logic.
- `/api/recipes`: Fetches `recipes` (supports filtering by cost, ingredients).
- `/api/grocery-lists`: Handles `CRUD` for `groceryLists` and `groceryListItems`.
- `/api/resources`: Fetches `localResources` (supports filtering by type, location).
**Key Functionalities & Logic:**
1. **User Authentication**: Implement secure user registration and login using NextAuth.js.
2. **Pantry Management**: Allow users to add, update, and delete ingredients they currently possess, specifying quantity, unit, expiration date (optional), and if it was obtained from a food bank.
3. **Budget Input**: Enable users to input their total monetary budget available for food for a specific period (e.g., two weeks, one month).
4. **Intelligent Meal Plan Generation**: This is central. The server-side function/API endpoint for `/api/meal-plans` should:
* Take into account the user's `total_budget` and current `pantryItems`.
* Prioritize using expiring items and items from the food bank first to minimize waste.
* Suggest `recipes` that are low-cost and nutrient-dense.
* Generate a full weekly or bi-weekly `mealPlan` (e.g., 3 meals a day).
* For MVP, the intelligence can be heuristic-based (e.g., prioritize recipes using a high percentage of pantry items, then low-cost staple recipes).
5. **Dynamic Grocery List Generation**: Based on the generated `mealPlan` and considering `pantryItems`, automatically create a `groceryList` of missing ingredients needed to execute the plan. Items should be grouped and quantities adjusted.
6. **Local Resource Locator**: Implement functionality to fetch and display `localResources` from the database. Integrate with a simple map library (e.g., Leaflet or Google Maps API for pins) to visualize locations. Allow users to search by location (e.g., zip code, city).
7. **Data Persistence**: All user-generated data (pantry, meal plans, grocery lists) must be securely stored and retrieved from the PostgreSQL database using Drizzle ORM.
**Instructions for AI:**
Provide the complete codebase for a Next.js 14 application that satisfies all the above requirements. This includes:
- Drizzle ORM schema definitions and migration scripts.
- Fully functional API routes with `CRUD` logic for all database entities.
- Front-end components and pages for each specified route, ensuring data fetching and submission interact correctly with the API routes.
- Authentication integration using NextAuth.js.
- A basic, but functional, server-side implementation for meal plan generation and grocery list creation (heuristics for MVP are fine).
- Basic UI/UX with Tailwind CS