As an expert Next.js developer, create a fully functional MVP for 'UserValueFlow', a platform designed to help SaaS companies manage and convert their free tier users. The application should be built using Next.js 14+ with the App Router, React, TypeScript, Tailwind CSS for styling, Drizzle ORM with a PostgreSQL database, and NextAuth.js for user authentication for the UserValueFlow platform itself. Implement a multi-page structure, comprehensive database schema, API routes, and full CRUD operations.
Project Name: UserValueFlow
Core Functionality:
1. **Client Onboarding**: SaaS companies can register, obtain an API key, and integrate their free user data.
2. **Free User Data Ingestion**: Client SaaS platforms can send free user behavior data (activity, feature usage, support tickets) to UserValueFlow via an API.
3. **Conversion Potential Scoring**: UserValueFlow automatically calculates and assigns a 'conversion potential score' to each free user based on ingested data.
4. **Feedback Management**: Ingest free user feedback (e.g., from support tickets, in-app forms). Filter and prioritize this feedback based on the user's conversion potential score.
5. **Automated Engagement Campaigns**: Allow client companies to create and manage automated campaigns (e.g., email sequences, in-app notifications) triggered by specific user behaviors or conversion scores.
6. **Analytics Dashboard**: Provide an overview of free user base, conversion potential distribution, and campaign performance.
Database Schema (Drizzle ORM for PostgreSQL):
* **companies** table:
* `id` (uuid, primary key, default `sql.gen_random_uuid()`)
* `name` (varchar(255), not null)
* `integrationKey` (varchar(255), not null, unique)
* `createdAt` (timestamp, default `sql.now()`, not null)
* `updatedAt` (timestamp, default `sql.now()`, onUpdate `sql.now()`, not null)
* **users** table (for UserValueFlow admins/client company users):
* `id` (uuid, primary key, default `sql.gen_random_uuid()`)
* `companyId` (uuid, foreign key reference `companies.id`, onDelete 'cascade')
* `email` (varchar(255), not null, unique)
* `passwordHash` (varchar(255), not null)
* `role` (varchar(50), default 'user', not null) (e.g., 'admin', 'company_user')
* `createdAt` (timestamp, default `sql.now()`, not null)
* `updatedAt` (timestamp, default `sql.now()`, onUpdate `sql.now()`, not null)
* **freeUsers** table (ingested from client SaaS):
* `id` (uuid, primary key, default `sql.gen_random_uuid()`)
* `clientSpecificUserId` (varchar(255), not null) (Client's internal user ID)
* `companyId` (uuid, foreign key reference `companies.id`, onDelete 'cascade', not null)
* `email` (varchar(255))
* `conversionScore` (decimal, default 0.0, not null)
* `lastActivity` (timestamp, default `sql.now()`, not null)
* `signupDate` (timestamp, not null)
* `supportTicketsCount` (integer, default 0, not null)
* `totalSessions` (integer, default 0, not null)
* `featureUsageData` (jsonb) (e.g., `{'featureX': 10, 'featureY': 5}`)
* `isTrial` (boolean, default false, not null)
* `trialEndDate` (timestamp, nullable)
* `createdAt` (timestamp, default `sql.now()`, not null)
* `updatedAt` (timestamp, default `sql.now()`, onUpdate `sql.now()`, not null)
* `unique('client_user_id_per_company', [clientSpecificUserId, companyId])`
* **feedback** table:
* `id` (uuid, primary key, default `sql.gen_random_uuid()`)
* `freeUserId` (uuid, foreign key reference `freeUsers.id`, onDelete 'cascade')
* `companyId` (uuid, foreign key reference `companies.id`, onDelete 'cascade')
* `message` (text, not null)
* `sentiment` (varchar(50), nullable) (e.g., 'positive', 'negative', 'neutral')
* `category` (varchar(100), nullable) (e.g., 'bug', 'feature request')
* `priorityScore` (decimal, default 0.0, not null) (derived from freeUser's conversionScore)
* `status` (varchar(50), default 'new', not null) (e.g., 'new', 'reviewed', 'actioned')
* `createdAt` (timestamp, default `sql.now()`, not null)
* **campaigns** table:
* `id` (uuid, primary key, default `sql.gen_random_uuid()`)
* `companyId` (uuid, foreign key reference `companies.id`, onDelete 'cascade')
* `name` (varchar(255), not null)
* `type` (varchar(50), not null) (e.g., 'email', 'in-app')
* `triggerConditions` (jsonb, not null) (e.g., `{'scoreMin': 0.7, 'lastActivityDaysMax': 7, 'featureX_used': true}`)
* `content` (text, not null) (e.g., email HTML or notification markdown)
* `isActive` (boolean, default true, not null)
* `createdAt` (timestamp, default `sql.now()`, not null)
* `updatedAt` (timestamp, default `sql.now()`, onUpdate `sql.now()`, not null)
Next.js App Router Structure:
* `/app` directory:
* `/(auth)`: Group for authentication pages
* `login/page.tsx`: User login form.
* `register/page.tsx`: User/Company registration form.
* `/(dashboard)`: Group for authenticated user pages with a shared layout.
* `layout.tsx`: Main dashboard layout with a sidebar for navigation (Dashboard, Users, Feedback, Campaigns, Settings) and a header.
* `page.tsx`: Dashboard homepage displaying key metrics (total free users, average conversion score, active campaigns, recent feedback).
* `users/page.tsx`: List of all free users for the authenticated company. Should be filterable and sortable by conversion score, activity, etc.
* `users/[id]/page.tsx`: Detailed view of a specific free user, showing their profile, behavior history, conversion score, associated feedback, and ability to manually adjust score or tag.
* `feedback/page.tsx`: List of all ingested feedback. Should be filterable by priority score, status, category, and searchable.
* `campaigns/page.tsx`: List of automated campaign