You are an expert full-stack developer tasked with building a Minimum Viable Product (MVP) for a productivity optimization SaaS application called 'Sleep Optimizer'. The application should help users understand their personal circadian rhythms and optimize their sleep-wake cycles for peak daily productivity, rather than forcing a rigid 'early bird' mentality. The MVP must be a multi-page application built with Next.js App Router (using the `app/` directory), utilizing Drizzle ORM for database interactions with PostgreSQL, and implementing robust API routes for full CRUD functionality. Do NOT generate a simple landing page or a basic SPA.
**Core Requirements:**
1. **Project Setup:** Use Next.js 13+ with the App Router (`app/` directory).
2. **Database:** PostgreSQL with Drizzle ORM for schema definition and interaction.
3. **Authentication:** Implement basic email/password authentication using Clerk or a similar service, integrated with NextAuth.js for session management.
4. **State Management:** Utilize React Context API or Zustand for client-side state management.
**Database Schema (Drizzle ORM):**
* **`users` table:**
* `id` (UUID, primary key)
* `email` (VARCHAR, unique)
* `passwordHash` (VARCHAR)
* `createdAt` (TIMESTAMP)
* `updatedAt` (TIMESTAMP)
* **`userProfiles` table:**
* `userId` (UUID, foreign key to `users.id`, unique)
* `name` (VARCHAR)
* `preferredWakeUpTime` (TIME)
* `naturalSleepPreference` (ENUM: 'night_owl', 'early_bird', 'flexible')
* `timezone` (VARCHAR)
* `createdAt` (TIMESTAMP)
* `updatedAt` (TIMESTAMP)
* **`sleepLogs` table:**
* `id` (UUID, primary key)
* `userId` (UUID, foreign key to `users.id`)
* `startTime` (TIMESTAMP)
* `endTime` (TIMESTAMP)
* `sleepQualityScore` (INTEGER, 1-5)
* `notes` (TEXT)
* `createdAt` (TIMESTAMP)
* **`dailyEnergyLevels` table:**
* `id` (UUID, primary key)
* `userId` (UUID, foreign key to `users.id`)
* `date` (DATE)
* `morningEnergy` (INTEGER, 1-10)
* `afternoonEnergy` (INTEGER, 1-10)
* `eveningEnergy` (INTEGER, 1-10)
* `notes` (TEXT)
* `createdAt` (TIMESTAMP)
* **`productivitySessions` table:**
* `id` (UUID, primary key)
* `userId` (UUID, foreign key to `users.id`)
* `startTime` (TIMESTAMP)
* `endTime` (TIMESTAMP)
* `taskDescription` (VARCHAR)
* `perceivedFocus` (INTEGER, 1-10)
* `createdAt` (TIMESTAMP)
**API Routes (`app/api/`):**
Implement CRUD operations for all tables. Examples:
* `POST /api/auth/signup`
* `POST /api/auth/login`
* `GET /api/user/profile`
* `PUT /api/user/profile`
* `POST /api/sleep-logs`
* `GET /api/sleep-logs`
* `GET /api/sleep-logs/:id`
* `PUT /api/sleep-logs/:id`
* `DELETE /api/sleep-logs/:id`
* `POST /api/energy-levels`
* `GET /api/energy-levels?date=<date>`
* `POST /api/productivity-sessions`
* `GET /api/productivity-sessions?startDate=<date>&endDate=<date>`
**Frontend Structure (`app/` directory):**
* **`layout.tsx`:** Root layout with basic structure and navigation.
* **`page.tsx`:** Dashboard page displaying an overview of sleep, energy, and productivity data, including personalized insights.
* **`auth/`:** Pages for signup, login.
* **`profile/`:** Pages for viewing and editing user profile (e.g., `profile/edit`).
* **`sleep/`:** Pages for logging sleep (`sleep/log`), viewing history (`sleep/history`).
* **`energy/`:** Pages for logging daily energy levels (`energy/log`), viewing trends (`energy/trends`).
* **`productivity/`:** Pages for logging productivity sessions (`productivity/log`), viewing analysis (`productivity/analysis`).
**MVP Features to Implement:**
1. **User Onboarding & Profile Setup:** Collect `preferredWakeUpTime`, `naturalSleepPreference`, and `timezone` upon signup.
2. **Sleep Logging:** Allow users to manually log their sleep start and end times, and rate sleep quality. API for CRUD operations.
3. **Daily Energy Logging:** Allow users to log their energy levels at different times of the day (morning, afternoon, evening). API for CRUD operations.
4. **Productivity Session Logging:** Allow users to log focused work sessions, including task description and perceived focus level. API for CRUD operations.
5. **Personalized Insights Dashboard:** Analyze logged data to provide:
* **Circadian Rhythm Analysis:** Estimate user's natural peak productivity times based on their logs and preferences (e.g., "Your peak focus tends to be between 10 AM and 1 PM.").
* **Sleep Quality Feedback:** Basic feedback on sleep duration and quality.
* **Energy Trend Visualization:** Charts showing energy levels throughout the day/week.
* **Productivity Correlation:** Show correlation between sleep/energy levels and productivity session focus.
6. **Basic Recommendations:** Offer simple, data-driven recommendations (e.g., "Consider adjusting your bedtime based on your energy dips.").
**Technical Considerations:**
* Use server components and client components appropriately.
* Ensure proper error handling for API calls and database operations.
* Implement basic UI/UX best practices for clarity and ease of use.
* Seed the database with sample data for initial testing if needed.
* The focus is on core logic and data management for the MVP, not on a polished, production-ready UI.