Build a full-stack web application using Next.js App Router, Drizzle ORM with PostgreSQL, and a React frontend. The application should help users manage and contest unexpected medical bills.
**Project Structure:**
- `app/` directory for routing and server components.
- `components/` for reusable UI components.
- `lib/` for utility functions and database connections.
- `server/` for API routes and backend logic.
- `styles/` for global CSS.
**Database Schema (Drizzle ORM):**
- `users` table: `id` (serial, primary key), `name` (text), `email` (text, unique), `hashedPassword` (text), `createdAt` (timestamp).
- `medicalBills` table: `id` (serial, primary key), `userId` (integer, foreign key to users.id), `providerName` (text), `serviceType` (text), `billedAmount` (decimal), `paidAmount` (decimal), `insuranceApplied` (decimal), `billDate` (date), `serviceDate` (date), `itemizedBillUrl` (text), `status` (enum: 'pending', 'analyzing', 'negotiating', 'paid', 'contested'), `createdAt` (timestamp).
- `billAnalysis` table: `id` (serial, primary key), `billId` (integer, foreign key to medicalBills.id), `analysisResult` (jsonb), `suggestions` (text), `createdAt` (timestamp).
- `negotiations` table: `id` (serial, primary key), `billId` (integer, foreign key to medicalBills.id), `negotiationAttempts` (integer), `lastContactDate` (date), `outcome` (text), `createdAt` (timestamp).
**Frontend (React with Next.js App Router):**
- Implement multi-page structure for:
- Home page (`/`)
- User Dashboard (`/dashboard`)
- Add New Bill (`/dashboard/bills/new`)
- View Bill Details (`/dashboard/bills/[id]`)
- Bill Analysis Results (`/dashboard/bills/[id]/analysis`)
- Negotiation Tracker (`/dashboard/bills/[id]/negotiation`)
- User Profile (`/dashboard/profile`)
- Authentication pages (Login, Signup).
- Use server components where appropriate for data fetching and SEO.
- Use client components for interactive elements (forms, buttons, charts).
- Implement robust form handling and validation for bill submission.
- Create a visually appealing and intuitive user interface.
- Integrate charting libraries to visualize spending and savings.
**Backend (API Routes & Server Logic):**
- Implement CRUD operations for all database tables.
- Create API routes under `server/api/` (e.g., `server/api/bills/route.ts`, `server/api/analysis/route.ts`).
- **Bill Analysis AI Integration:** Develop a backend service that takes bill details and uses an AI model (e.g., text analysis, rule-based system) to identify potential discrepancies, overcharges, and suggest negotiation strategies. This service should populate the `billAnalysis` table.
- **Negotiation Logic:** Implement logic to track negotiation progress, log attempts, and store outcomes, populating the `negotiations` table.
- **Authentication:** Implement secure user authentication using NextAuth.js or a similar library.
- **Error Handling:** Implement comprehensive error handling for API requests and backend processes.
**Key Features to Implement:**
1. **User Authentication:** Secure signup and login.
2. **Bill Submission:** Form to input detailed medical bill information, including the ability to upload an itemized bill (e.g., PDF/image, store URL in `itemizedBillUrl`).
3. **Bill Dashboard:** A view listing all user's medical bills with their current status.
4. **AI-Powered Analysis:** Upon bill submission, trigger an asynchronous analysis process. The analysis should compare billed amounts against typical costs (initially placeholder, later potentially external API integration), flag anomalies, and provide actionable suggestions for negotiation or dispute. Store results in `billAnalysis`.
5. **Negotiation Tracking:** Allow users to manually update negotiation status and details, or have the system prompt for updates. Log negotiation attempts.
6. **Data Persistence:** Ensure all data is correctly stored and retrieved from the PostgreSQL database via Drizzle ORM.
7. **API Endpoints:** Create endpoints for fetching bills, submitting new bills, triggering analysis, updating bill status, and managing user data.
**Development Stack:**
- **Framework:** Next.js (App Router)
- **Database:** PostgreSQL
- **ORM:** Drizzle ORM
- **Language:** TypeScript
- **Frontend:** React, Tailwind CSS (for styling)
- **Authentication:** NextAuth.js (recommended)
- **State Management:** Zustand or React Context API