Create a comprehensive Next.js App Router (app directory) based SaaS application for Etsy sellers to manage customer disputes and refund requests. The application should be a multi-page platform with full CRUD (Create, Read, Update, Delete) functionality for all core features.
**Database Schema (Drizzle ORM with PostgreSQL):**
1. **Users Table:** id (UUID, primary key), email (unique), password (hashed), name, marketplace_preference (e.g., 'etsy', 'ebay'), created_at, updated_at.
2. **Products Table:** id (UUID, primary key), user_id (foreign key to Users), product_name, product_description, sku, marketplace_url, created_at, updated_at.
3. **Disputes Table:** id (UUID, primary key), user_id (foreign key to Users), product_id (foreign key to Products, nullable), order_id (string, nullable), customer_name, customer_email, dispute_type (e.g., 'refund_request', 'complaint'), reason, status ('open', 'resolved', 'escalated'), received_at, created_at, updated_at.
4. **Communications Table:** id (UUID, primary key), dispute_id (foreign key to Disputes), sender_type ('customer', 'seller'), message_text, sent_at, created_at.
5. **Evidence Table:** id (UUID, primary key), dispute_id (foreign key to Disputes), file_url (string), file_type ('image', 'video', 'document'), uploaded_at, created_at.
6. **ResponseTemplates Table:** id (UUID, primary key), user_id (foreign key to Users), template_name, template_body, applicable_dispute_types (array of strings).
**Application Structure (Next.js App Router):**
* **`/app/` directory:**
* `layout.tsx`: Root layout with authentication context.
* `page.tsx`: Dashboard landing page after login, showing a summary of open disputes and recent activity.
* `dashboard/page.tsx`: User dashboard with quick stats.
* `disputes/page.tsx`: List all disputes. Include filtering and sorting options (by status, date, product).
* `disputes/[disputeId]/page.tsx`: View a single dispute. This page should contain:
* Dispute details.
* Communication log (CRUD for messages).
* Evidence management (CRUD for uploads).
* Option to add response templates or write custom responses.
* Status update functionality.
* `products/page.tsx`: List all seller's products. CRUD for products.
* `templates/page.tsx`: Manage response templates. CRUD for templates.
* `settings/page.tsx`: User account and marketplace settings.
* `auth/login/page.tsx`, `auth/signup/page.tsx`: Authentication pages.
**API Routes (`/app/api/`):**
* `/api/auth/...`: Authentication endpoints (login, signup, logout).
* `/api/disputes`: CRUD operations for disputes.
* `/api/disputes/[disputeId]/communications`: CRUD operations for communications within a dispute.
* `/api/disputes/[disputeId]/evidence`: CRUD operations for evidence uploads within a dispute.
* `/api/templates`: CRUD operations for response templates.
* `/api/products`: CRUD operations for products.
* `/api/users/me`: Get current user details.
**Core Functionalities to Implement:**
1. **User Authentication & Authorization:** Secure login/signup. Protect routes based on authentication status.
2. **Dispute Management:** Create, view, update (status, details), and delete disputes. Implement a clear workflow for dispute resolution.
3. **Communication:** Allow sellers to add customer messages and send responses. Display communication history chronologically.
4. **Evidence Handling:** Implement file upload functionality for images, videos, and documents. Store file URLs in the database and handle file storage (e.g., using AWS S3 or a similar service).
5. **Response Templates:** Allow sellers to create, save, and use predefined response templates for common scenarios. Implement a system to select and insert templates into responses.
6. **Product Catalog:** Basic CRUD for seller's products to associate with disputes.
7. **Dashboard Summary:** Provide a quick overview of key metrics (e.g., number of open disputes, recent communications).
8. **Database Integration:** Utilize Drizzle ORM for efficient and type-safe database interactions with PostgreSQL.
9. **Error Handling & Validation:** Implement robust error handling for API requests and form submissions. Client-side and server-side validation.
10. **Responsive UI:** Ensure the application is usable on different screen sizes.