You are an expert full-stack developer tasked with building a robust Minimum Viable Product (MVP) for 'Data Safety Shield' (Veri Güvenliği Kalkanı). The application's primary goal is to prevent accidental data loss in production databases due to developer errors during migration or operational scripts. The MVP should be built using Next.js App Router, TypeScript, Drizzle ORM with PostgreSQL, and Tailwind CSS for styling.
**Core Requirements:**
1. **Project Structure:** Utilize the Next.js App Router (`app/` directory) for multi-page routing and server components.
2. **Database:** Use PostgreSQL as the database. Integrate Drizzle ORM for type-safe database interactions. Define the schema for the following tables:
* `users`: id, email, password (hashed), name, company_id (for multi-tenancy)
* `companies`: id, name, subscription_plan (e.g., 'free', 'pro', 'enterprise')
* `database_connections`: id, user_id, company_id, db_type (e.g., 'postgres'), connection_string (encrypted), alias, is_production (boolean)
* `activity_logs`: id, company_id, connection_alias, script_name, action_type (e.g., 'DROP_TABLE', 'ALTER_TABLE', 'DELETE_DATA'), timestamp, user_id (who initiated)
* `alerts`: id, company_id, connection_alias, alert_type (e.g., 'UNAUTHORIZED_ACCESS', 'DESTRUCTIVE_OPERATION'), message, status ('open', 'closed'), timestamp
3. **Authentication:** Implement secure user authentication using NextAuth.js. Include email/password login and session management.
4. **Database Connections Management (CRUD):**
* Users should be able to add, view, edit, and delete their database connections.
* Support for PostgreSQL connections initially. Connection strings must be encrypted at rest.
* Clearly distinguish between production and staging/development connections.
5. **Real-time Replication (Simulated MVP Feature):** For the MVP, simulate the real-time replication aspect. Instead of actual replication, focus on logging every single SQL query executed against a *connected* production database and store it in `activity_logs`. This log should be as close to real-time as possible using polling or WebSockets if feasible for a demo. **Note:** For a true MVP, this might be a background job polling for changes or a listener if the DB supports it. For the AI build, logging executed queries is the core task.
6. **Destructive Operation Protection (CLI Tool/API Endpoint):**
* Develop a secure API endpoint (e.g., `/api/run-script`) that accepts a script and a target database connection alias.
* Before executing any script flagged as 'destructive' (e.g., containing `DROP`, `DELETE`, `ALTER`), the API must perform a secondary check.
* This check could be a user confirmation step via a modal in the UI or a specific confirmation token passed in the API request.
* **CLI Integration:** Create a simple CLI tool (e.g., `shield-cli`) that wraps database commands. When executed, it should first check if the target is a production database connection configured in the app. If so, it should prompt the user for confirmation with a specific warning message (e.g., "WARNING: Executing destructive command on PRODUCTION database '{alias}'. Type 'CONFIRM' to proceed."). This CLI tool should communicate with the backend API to log the operation and potentially trigger the confirmation flow.
7. **Activity Logging:** Log all significant database operations (migrations, scripts, DDL/DML) to the `activity_logs` table. Include details like the user, timestamp, script name/content snippet, action type, and target database connection.
8. **Alerting System:**
* Implement basic alert generation based on specific patterns in `activity_logs` (e.g., multiple `DELETE` operations in a short period, `DROP` operations on production).
* Display these alerts in a dedicated `/alerts` page within the application.
* Users should be able to view and mark alerts as resolved.
9. **Backup Simulation (Hourly):**
* Create a background job or a scheduled API endpoint that simulates taking an hourly backup. This can be a simple function that logs "Simulated hourly backup for connection {alias} at {timestamp}" to the `activity_logs` or a dedicated backup log table.
* Implement a basic "Restore from Backup" functionality (simulated) on the `/connections` page, allowing users to select a timestamp and trigger a restore process (which again, just logs the action for the MVP).
10. **UI/UX:**
* A clean, intuitive dashboard showing connected databases, recent activity, and open alerts.
* Dedicated pages for managing database connections, viewing detailed activity logs, and managing alerts.
* Use Tailwind CSS for styling.
11. **API Routes:** Implement necessary API routes for all CRUD operations, authentication, running scripts, and fetching logs/alerts.
12. **Error Handling:** Implement robust error handling throughout the application, especially for database operations and API requests.
**Technology Stack:**
* **Frontend:** Next.js (App Router), TypeScript, Tailwind CSS
* **Backend:** Next.js API Routes, TypeScript
* **Database:** PostgreSQL
* **ORM:** Drizzle ORM
* **Authentication:** NextAuth.js
* **CLI (Optional but Recommended):** Node.js/TypeScript for `shield-cli`.