You are a senior full-stack developer tasked with building a Minimum Viable Product (MVP) for 'Nomad Compass', a platform helping digital nomads find and compare locations based on specific criteria. The application must be built using Next.js App Router (app/ directory), Drizzle ORM for database interactions, and Tailwind CSS for styling. It should be a multi-page application with full CRUD functionality where applicable.
Here's a detailed breakdown of the requirements:
**1. Project Setup & Core Technologies:**
* Initialize a Next.js project with App Router.
* Set up Drizzle ORM with a PostgreSQL database (use a simple `DATABASE_URL` environment variable).
* Configure Tailwind CSS for styling.
* Implement basic user authentication (register, login, logout) using a simple session-based or JWT approach.
**2. Database Schema (Drizzle ORM):**
Create the following schemas in `drizzle/schema.ts`:
* `users`: `id` (uuid, primary key), `email` (string, unique), `passwordHash` (string), `username` (string, unique), `createdAt` (timestamp).
* `locations`: `id` (uuid, primary key), `name` (string), `country` (string), `avgCostOfLiving` (number, float), `safetyScore` (number, float, 0-10), `internetReliability` (number, float, 0-10), `electricityReliability` (number, float, 0-10), `foodSafetyScore` (number, float, 0-10), `airQualityScore` (number, float, 0-10), `description` (text), `latitude` (number, float), `longitude` (number, float), `imageUrl` (string, optional).
* `reviews`: `id` (uuid, primary key), `userId` (uuid, foreign key to `users`), `locationId` (uuid, foreign key to `locations`), `overallRating` (number, float, 1-5), `safetyRating` (number, float, 1-5), `internetRating` (number, float, 1-5), `foodRating` (number, float, 1-5), `electricityRating` (number, float, 1-5), `airRating` (number, float, 1-5), `comment` (text), `createdAt` (timestamp).
* `favorites`: `id` (uuid, primary key), `userId` (uuid, foreign key to `users`), `locationId` (uuid, foreign key to `locations`), `createdAt` (timestamp).
**3. API Routes (app/api directory):**
Implement the following API endpoints:
* `GET /api/auth/me`: Get current user info (requires authentication).
* `POST /api/auth/register`: Register a new user (receives email, username, password).
* `POST /api/auth/login`: Authenticate user (receives email/username, password, returns token/session).
* `POST /api/auth/logout`: Log out user.
* `GET /api/locations`: Fetch all locations. Support query parameters for filtering: `country`, `minCost`, `maxCost`, `minSafety`, `minInternet`, `minFood`, `minAir`. Implement sorting by any of these fields.
* `GET /api/locations/[id]`: Fetch a single location by ID.
* `POST /api/locations`: Create a new location (Admin-only access check is required). Implement data validation.
* `PUT /api/locations/[id]`: Update an existing location (Admin-only access check). Implement data validation.
* `DELETE /api/locations/[id]`: Delete a location (Admin-only access check).
* `GET /api/reviews/[locationId]`: Fetch all reviews for a specific location.
* `POST /api/reviews`: Submit a new review for a location (requires authenticated user). Implement data validation.
* `GET /api/users/[userId]/favorites`: Fetch a user's favorite locations (requires user authentication and ownership check).
* `POST /api/favorites`: Add a location to user's favorites (requires authenticated user, receives `locationId`).
* `DELETE /api/favorites/[id]`: Remove a location from user's favorites (requires authenticated user and ownership check).
**4. Frontend Pages (app/ directory):**
* `app/page.tsx` (Homepage): Display a prominent search bar and potentially a list of featured or recently added locations. Include navigation links to login/register/dashboard.
* `app/auth/login/page.tsx`: User login form.
* `app/auth/register/page.tsx`: User registration form.
* `app/locations/page.tsx`: A list of all locations. Implement a sidebar with filters (country, cost range, safety score range, internet score, food safety score, air quality score) and a search input. Display location cards with key info (name, country, avg cost, safety score). Use client-side filtering/pagination based on API responses.
* `app/locations/[id]/page.tsx`: Detailed view for a single location. Display all details, average scores from reviews, and a section for user reviews. If authenticated, allow users to submit a new review.
* `app/dashboard/page.tsx`: User dashboard. Display the user's favorite locations, saved searches, and basic profile information.
* `app/admin/locations/page.tsx`: (Optional but good for full CRUD) A page for administrators to list, add, edit, and delete locations. This will demonstrate the full CRUD for locations.
**5. Component Design:**
* Create reusable UI components like `LocationCard`, `FilterSidebar`, `SearchBar`, `ReviewForm`, `AuthForm`.
* Ensure responsive design using Tailwind CSS.
**6. Data Management:**
* Handle API calls from client components using `fetch` or a library like `react-query` (optional but good practice).
* Manage user authentication state across the application.
**Key Instructions for AI:**
* Focus on functional completeness for the MVP, ensuring all specified CRUD operations and API routes work correctly.
* Use modern Next.js App Router patterns (Server Components, Client Components where appropriate).
* Provide realistic placeholder data for the database if necessary for initial testing.
* Include error handling for API responses and user input validation on both frontend and backend.
* Do not create a simple landing page or a Single Page Application without multi-page navigation. The application must have distinct routes and pages as described.