You are an AI assistant tasked with building a full-stack web application for managing financial disputes, specifically focusing on chargebacks and refunds. The application should be built using Next.js with the App Router, TypeScript, Drizzle ORM with PostgreSQL, and Tailwind CSS for styling.
**Core Functionality:**
1. **User Authentication:** Implement secure user authentication using NextAuth.js (or a similar robust solution). Users should be able to sign up, log in, and manage their profile.
2. **Dispute Management:** Users should be able to create, view, update, and delete (CRUD) financial disputes. Each dispute should have the following attributes:
* `userId`: Foreign key to the user.
* `disputeType`: Enum (e.g., CHARGEBACK, REFUND, FRAUD).
* `financialInstitution`: String (e.g., 'Amex', 'Visa', 'Mastercard', 'Chase Bank').
* `merchantName`: String.
* `transactionDate`: Date.
* `transactionAmount`: Number.
* `reason`: Text description of the dispute reason.
* `status`: Enum (e.g., 'PENDING', 'UNDER_REVIEW', 'APPROVED', 'REJECTED', 'CREDITED').
* `createdAt`: Date.
* `updatedAt`: Date.
3. **Document Uploads:** Users must be able to upload multiple documents (e.g., receipts, emails, screenshots) associated with each dispute. Implement secure file storage (e.g., using AWS S3 or a similar cloud storage service). Each document should have:
* `disputeId`: Foreign key to the dispute.
* `fileName`: String.
* `fileUrl`: String (URL to the stored file).
* `uploadedAt`: Date.
4. **Communication Tracking:** Allow users to log communications related to a dispute (e.g., emails, phone calls). Each communication entry should have:
* `disputeId`: Foreign key to the dispute.
* `communicationDate`: Date.
* `type`: Enum (e.g., 'EMAIL', 'PHONE_CALL', 'LETTER').
* `content`: Text area for details.
* `contactPerson` (Optional): String.
5. **Template Generation (MVP - Basic):** For MVP, implement a basic feature to display pre-defined communication templates based on dispute type and financial institution. Future iterations can involve dynamic generation and user customization.
6. **Dashboard:** A user dashboard displaying a summary of all disputes, their statuses, and recent activities.
**Technical Stack & Requirements:**
* **Framework:** Next.js App Router (`app/` directory).
* **Language:** TypeScript.
* **Database:** PostgreSQL with Drizzle ORM for type-safe database interactions. Define schemas for `users`, `disputes`, `documents`, and `communications` tables.
* **Authentication:** NextAuth.js with credentials provider and potentially OAuth providers (Google, etc.).
* **Styling:** Tailwind CSS.
* **API Routes:** Implement API routes within the `app/api/` directory for all CRUD operations and file uploads.
* **Error Handling:** Robust error handling across the application and API routes.
* **Deployment:** Consider Vercel for deployment.
**Project Structure:**
* `app/`: Main Next.js App Router directory.
* `(auth)/`: Routes for authentication (login, signup).
* `(dashboard)/`: Protected routes for authenticated users (dashboard, disputes, documents, etc.).
* `api/`: API routes for backend logic.
* `[disputeId]/`: Dynamic API routes for specific disputes.
* `auth/`: API routes for authentication.
* `upload/`: API route for file uploads.
* `components/`: Reusable UI components.
* `lib/`: Utility functions, database connections (Drizzle config), API clients.
* `styles/`: Global styles.
* `types/`: TypeScript type definitions.
**Implementation Details:**
* Set up Drizzle ORM with PostgreSQL. Define the database schema migrations.
* Implement API endpoints for:
* User registration and login.
* Creating, fetching (list and single), updating, and deleting disputes.
* Uploading documents (handle multipart/form-data).
* Logging and fetching communications.
* Create protected routes that require user authentication.
* Build the UI components using React and Tailwind CSS for a responsive and clean interface.
* Ensure data validation on both client-side and server-side (API routes).
* Provide clear feedback to the user for all actions (loading states, success/error messages).
**Focus on a complete MVP, not just a landing page or a simple SPA. This means functional CRUD operations, database integration, and user authentication.**