You are an expert full-stack developer tasked with building a Minimum Viable Product (MVP) for 'Contract Guardian (Sözleşme Güvencesi)', a SaaS application designed to help small businesses and freelancers analyze contracts for intellectual property (IP) assignment risks and auto-renewal pitfalls. The application should leverage AI for analysis and provide clear, actionable insights.
**Project Setup:**
- Use **Next.js App Router** (app/ directory).
- Implement a **multi-page structure** for navigation (e.g., dashboard, upload, analysis results, history, settings).
- Use **TypeScript** for strong typing.
- Set up **Drizzle ORM** with **PostgreSQL** as the database. Ensure proper schema definition for users, contracts, and analysis results.
- Use **Tailwind CSS** for styling.
**Core Features and Functionality (MVP):**
1. **User Authentication:**
* Implement secure user registration and login using NextAuth.js or a similar library.
* Protect routes and API endpoints based on authentication status.
2. **Contract Upload:**
* Create a user interface for uploading contract documents (PDF, DOCX).
* Store uploaded contracts in cloud storage (e.g., AWS S3, Vercel Blob) and record metadata (filename, upload date, user ID) in the database.
3. **AI-Powered Contract Analysis (Core Logic):**
* **IP Assignment Risk Analysis:** Analyze contract text for clauses that could lead to unintended assignment of intellectual property (e.g., 'ownership of tools or processes developed during the engagement', 'used in connection with the work').
* **Auto-Renewal & Cancellation Risk Analysis:** Analyze for clauses related to automatic renewal and cancellation windows (e.g., 'auto-renewal with a 90-day cancellation window').
* **Integration:** Integrate with an AI model (e.g., OpenAI's GPT API, Google Gemini API) to process the contract text and identify these specific risks. The AI should be prompted to look for keywords, sentence structures, and common legal phrasing associated with these risks.
* **Output:** The AI should return structured data indicating the presence and severity of each identified risk, along with a brief explanation.
4. **Analysis Results Display:**
* Create a dedicated page to display the results of the contract analysis.
* Clearly present the identified risks, explanations, and potential impact.
* Provide a summary of the overall risk score or assessment.
5. **Contract History:**
* Implement a dashboard or history page where users can view all previously uploaded and analyzed contracts.
* Allow users to revisit analysis results.
**Database Schema (Drizzle ORM - PostgreSQL):**
- **`users` table:** `id` (UUID, primary key), `name` (TEXT), `email` (TEXT, unique), `hashedPassword` (TEXT), `createdAt` (TIMESTAMP).
- **`contracts` table:** `id` (UUID, primary key), `userId` (UUID, foreign key to `users.id`), `fileName` (TEXT), `storageUrl` (TEXT), `uploadedAt` (TIMESTAMP).
- **`analysisResults` table:** `id` (UUID, primary key), `contractId` (UUID, foreign key to `contracts.id`), `ipAssignmentRisk` (BOOLEAN), `ipAssignmentExplanation` (TEXT), `autoRenewalRisk` (BOOLEAN), `autoRenewalExplanation` (TEXT), `overallRiskScore` (INTEGER), `generatedAt` (TIMESTAMP).
**API Routes (app/api/):**
- `/api/auth/*`: For authentication endpoints (handled by NextAuth.js).
- `/api/upload`: POST endpoint to handle contract uploads, process them, and trigger AI analysis.
- `/api/contracts`: GET endpoint to fetch a user's contract history.
- `/api/contracts/[id]`: GET endpoint to fetch details and analysis results for a specific contract.
**Development Steps:**
1. Initialize Next.js project with TypeScript and Tailwind CSS.
2. Set up Drizzle ORM with PostgreSQL and define the initial schemas.
3. Implement user authentication using NextAuth.js.
4. Create the contract upload component and API endpoint, integrating with cloud storage.
5. Develop the AI integration logic:
* Create helper functions to extract text from uploaded documents (consider libraries like `pdf-parse` for PDFs).
* Craft specific, detailed prompts for the AI model to identify IP and auto-renewal risks.
* Implement the API route to call the AI service and store the results.
6. Build the UI for displaying analysis results and contract history.
7. Ensure full CRUD operations for contracts and their analysis results via API routes and UI.
8. Implement proper error handling and loading states throughout the application.
**Crucially, the MVP must demonstrate the full backend logic for contract processing, AI interaction, and data persistence, not just a frontend interface.**