Index: .env
===================================================================
--- .env	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ .env	(revision 92c45a3d4ce7df33941d34706b6684bb837042b7)
@@ -15,2 +15,3 @@
 
 
+
Index: prisma/seed.js
===================================================================
--- prisma/seed.js	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ prisma/seed.js	(revision 92c45a3d4ce7df33941d34706b6684bb837042b7)
@@ -1,56 +1,5 @@
 import { PrismaClient } from '@prisma/client';
-import fs from 'fs';
-import path from 'path';
 
 const prisma = new PrismaClient();
-
-async function applyAdvancedSQLFeatures(prisma) {
-  console.log('🔧 Applying Advanced SQL Features...');
-
-  try {
-    // Apply Complex Invoice Status Trigger
-    console.log('📊 Applying Invoice Status Trigger...');
-    const triggerSQL = fs.readFileSync(
-      path.join(process.cwd(), 'sql/01_invoice_status_trigger.sql'),
-      'utf8'
-    );
-    
-    await prisma.$executeRawUnsafe(triggerSQL);
-    console.log('✅ Invoice status trigger applied successfully!');
-
-    // Apply Client Email Index Optimization
-    console.log('📈 Applying Client Email Index Optimization...');
-    const indexSQL = fs.readFileSync(
-      path.join(process.cwd(), 'sql/02_client_email_index.sql'),
-      'utf8'
-    );
-    
-    await prisma.$executeRawUnsafe(indexSQL);
-    console.log('✅ Email index optimization applied successfully!');
-
-    // Verify implementations
-    const triggerCheck = await prisma.$queryRaw`
-      SELECT tgname FROM pg_trigger WHERE tgname = 'invoice_status_update_trigger'
-    `;
-    
-    const indexCheck = await prisma.$queryRaw`
-      SELECT count(*) as index_count
-      FROM pg_indexes 
-      WHERE tablename = 'Client' AND indexname LIKE 'idx_client_email%'
-    `;
-
-    console.log(`✅ Trigger verification: ${triggerCheck.length > 0 ? 'Active' : 'Not found'}`);
-    console.log(`✅ Index verification: ${indexCheck[0].index_count} email indexes created`);
-
-    return true;
-  } catch (error) {
-    console.error('❌ Error applying advanced SQL features:', error.message);
-    
-    // Don't fail the entire seeding process for SQL features
-    // This allows the basic seeding to complete even if SQL features fail
-    console.log('⚠️  Continuing with basic seeding...');
-    return false;
-  }
-}
 
 async function main() {
@@ -140,17 +89,4 @@
   console.log('Seeded default tenant:', defaultTenant);
   console.log('Seeded default user:', defaultUser);
-
-  // Apply advanced SQL features after basic seeding
-  const sqlFeaturesApplied = await applyAdvancedSQLFeatures(prisma);
-
-  console.log('✅ Seeding complete!');
-  
-  if (sqlFeaturesApplied) {
-    console.log('🎉 Advanced SQL features also applied!');
-    console.log('   • Complex invoice status trigger with business logic');
-    console.log('   • Optimized email indexes for Client table');
-  } else {
-    console.log('ℹ️  Advanced SQL features skipped (check sql/ directory exists)');
-  }
 }
 
Index: sql/01_invoice_status_trigger.sql
===================================================================
--- sql/01_invoice_status_trigger.sql	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ sql/01_invoice_status_trigger.sql	(revision 92c45a3d4ce7df33941d34706b6684bb837042b7)
@@ -1,4 +1,3 @@
--- Complex Trigger for Invoice Status Updates
--- This trigger fires when an invoice status is updated and performs multiple complex operations
+-- This trigger fires when an invoice status is updated
 
 -- First, create a table to log invoice status changes and history
@@ -15,5 +14,5 @@
 );
 
--- Create a notifications table for complex business logic
+-- Create a notifications table
 CREATE TABLE IF NOT EXISTS "InvoiceNotifications" (
     id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
@@ -85,6 +84,10 @@
         WHEN 'PAID' THEN
             notification_message := format(
-                'Invoice %s (€%.2f) has been PAID! Client total outstanding: €%.2f across %s invoices. Monthly revenue: €%.2f',
-                NEW.invoice_number, NEW.total_amount, client_total_outstanding, client_invoice_count, tenant_monthly_revenue
+                'Invoice %s (€%s) has been PAID! Client total outstanding: €%s across %s invoices. Monthly revenue: €%s',
+                NEW.invoice_number, 
+                to_char(NEW.total_amount, 'FM999999999.00'),
+                to_char(client_total_outstanding, 'FM999999999.00'),
+                client_invoice_count,
+                to_char(tenant_monthly_revenue, 'FM999999999.00')
             );
             priority_level := 'LOW';
@@ -93,6 +96,6 @@
             IF client_total_outstanding = 0 THEN
                 UPDATE "Client" 
-                SET status = 'ACTIVE'::ClientStatus 
-                WHERE id = NEW.client_id AND status != 'ACTIVE'::ClientStatus;
+                SET status = 'ACTIVE'
+                WHERE id = NEW.client_id AND status != 'ACTIVE';
                 
                 notification_message := notification_message || ' Client status updated to ACTIVE.';
@@ -101,6 +104,10 @@
         WHEN 'OVERDUE' THEN
             notification_message := format(
-                'CRITICAL: Invoice %s (€%.2f) is now OVERDUE (%s days since issue). Client owes €%.2f total across %s invoices.',
-                NEW.invoice_number, NEW.total_amount, days_since_issue, client_total_outstanding, client_invoice_count
+                'CRITICAL: Invoice %s (€%s) is now OVERDUE (%s days since issue). Client owes €%s total across %s invoices.',
+                NEW.invoice_number,
+                to_char(NEW.total_amount, 'FM999999999.00'),
+                days_since_issue,
+                to_char(client_total_outstanding, 'FM999999999.00'),
+                client_invoice_count
             );
             priority_level := 'HIGH';
@@ -109,5 +116,5 @@
             IF client_invoice_count > 3 THEN
                 UPDATE "Client" 
-                SET status = 'INACTIVE'::ClientStatus 
+                SET status = 'INACTIVE'
                 WHERE id = NEW.client_id;
                 
@@ -117,12 +124,16 @@
         WHEN 'PROCESSING' THEN
             notification_message := format(
-                'Invoice %s (€%.2f) is now being processed. Days since issue: %s',
-                NEW.invoice_number, NEW.total_amount, days_since_issue
+                'Invoice %s (€%s) is now being processed. Days since issue: %s',
+                NEW.invoice_number,
+                to_char(NEW.total_amount, 'FM999999999.00'),
+                days_since_issue
             );
 
         WHEN 'PENDING' THEN
             notification_message := format(
-                'Invoice %s (€%.2f) is pending payment. Client total outstanding: €%.2f',
-                NEW.invoice_number, NEW.total_amount, client_total_outstanding
+                'Invoice %s (€%s) is pending payment. Client total outstanding: €%s',
+                NEW.invoice_number,
+                to_char(NEW.total_amount, 'FM999999999.00'),
+                to_char(client_total_outstanding, 'FM999999999.00')
             );
             
@@ -140,5 +151,5 @@
     END CASE;
 
-    -- Insert notification for complex business rules
+    -- Insert notification
     INSERT INTO "InvoiceNotifications" (
         invoice_id,
Index: sql/02_client_email_index.sql
===================================================================
--- sql/02_client_email_index.sql	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ sql/02_client_email_index.sql	(revision 92c45a3d4ce7df33941d34706b6684bb837042b7)
@@ -5,15 +5,8 @@
 DROP INDEX IF EXISTS idx_client_email_basic;
 
--- Create a B-tree index for exact email matches
--- B-tree is optimal for equality queries and is the default index type in PostgreSQL
-CREATE INDEX IF NOT EXISTS idx_client_email_btree ON "Client"(email);
+-- Index for client email queries
+CREATE INDEX IF NOT EXISTS idx_client_email ON "Client"(email);
 
--- Create a partial index for active clients only (most common use case)
--- This is smaller and faster for queries filtering active clients by email
-CREATE INDEX IF NOT EXISTS idx_client_email_active ON "Client"(email) 
-WHERE status = 'ACTIVE';
-
--- Create a composite index for email + tenant_id (multi-tenant optimization)
--- This covers queries that filter by both email and tenant
+-- Index for email + tenant lookups
 CREATE INDEX IF NOT EXISTS idx_client_email_tenant ON "Client"(email, tenant_id);
 
@@ -22,25 +15,4 @@
 CREATE INDEX IF NOT EXISTS idx_client_email_lower ON "Client"(LOWER(email));
 
--- Performance analysis queries (for testing the indexes)
--- You can run these to verify the indexes are being used:
-
-/*
--- Test exact email match (should use idx_client_email_btree)
-EXPLAIN (ANALYZE, BUFFERS) 
-SELECT * FROM "Client" WHERE email = 'test@example.com';
-
--- Test case-insensitive email match (should use idx_client_email_lower)
-EXPLAIN (ANALYZE, BUFFERS) 
-SELECT * FROM "Client" WHERE LOWER(email) = LOWER('Test@Example.com');
-
--- Test email + tenant lookup (should use idx_client_email_tenant)
-EXPLAIN (ANALYZE, BUFFERS) 
-SELECT * FROM "Client" WHERE email = 'test@example.com' AND tenant_id = 'some-tenant-id';
-
--- Test active client email lookup (should use idx_client_email_active)
-EXPLAIN (ANALYZE, BUFFERS) 
-SELECT * FROM "Client" WHERE email = 'test@example.com' AND status = 'ACTIVE';
-*/
-
 -- Update table statistics for query planner optimization
 ANALYZE "Client"; 
Index: l/IMPLEMENTATION_GUIDE.md
===================================================================
--- sql/IMPLEMENTATION_GUIDE.md	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ 	(revision )
@@ -1,216 +1,0 @@
-# Implementation Approaches - Advanced SQL Features
-
-This guide explains the different ways to implement advanced SQL features in your Prisma project and when to use each approach.
-
-## 🎯 Three Implementation Approaches
-
-### 1. Integrated with Seed (`prisma/seed.js`) ✅ RECOMMENDED
-
-**Best for**: Development and initial setup
-
-```bash
-npm run db:seed
-# This will:
-# 1. Seed your database with initial data
-# 2. Apply advanced SQL features (triggers + indexes)
-# 3. Verify everything is working
-```
-
-**Pros**:
-
-- ✅ One command does everything
-- ✅ Perfect for development setup
-- ✅ Ensures SQL features are applied with initial data
-- ✅ Non-blocking (continues if SQL features fail)
-
-**Cons**:
-
-- ⚠️ Re-runs SQL features every time you seed
-- ⚠️ Might not be suitable for production deployments
-
-### 2. Separate Script (`sql/apply-advanced-sql.js`)
-
-**Best for**: Production deployments and maintenance
-
-```bash
-node sql/apply-advanced-sql.js
-```
-
-**Pros**:
-
-- ✅ Dedicated purpose - only applies SQL features
-- ✅ Can be run independently of seeding
-- ✅ Better for production deployment scripts
-- ✅ More granular control
-
-**Cons**:
-
-- ⚠️ Requires running multiple commands
-- ⚠️ Need to remember to run it separately
-
-### 3. Prisma Migration (Most Professional) 🏆 BEST FOR PRODUCTION
-
-**Best for**: Production environments and team collaboration
-
-```bash
-# Create migration
-npx prisma migrate dev --create-only --name advanced-sql-features
-
-# Copy SQL content to migration file, then apply
-npx prisma migrate dev
-```
-
-**Pros**:
-
-- ✅ Version controlled database changes
-- ✅ Automatic deployment with migrate deploy
-- ✅ Rollback capabilities
-- ✅ Team collaboration friendly
-- ✅ Production-ready
-
-**Cons**:
-
-- ⚠️ More setup steps initially
-- ⚠️ Need to manually copy SQL to migration files
-
-## 📋 What Goes Where?
-
-### ✅ Can be in `schema.prisma`:
-
-```prisma
-model Client {
-  email String @unique
-
-  // Basic indexes - Prisma handles these
-  @@index([email])
-  @@index([email, tenantId])
-  @@index([status])
-}
-```
-
-### ❌ Must be Raw SQL:
-
-- Complex triggers with business logic
-- Partial indexes (`WHERE status = 'ACTIVE'`)
-- Functional indexes (`LOWER(email)`)
-- Database functions
-- Advanced constraints
-
-## 🚀 Recommended Setup by Environment
-
-### Development Environment
-
-```bash
-# Option 1: All-in-one (easiest)
-npm run db:seed
-
-# Option 2: Step by step
-npx prisma migrate dev
-node sql/apply-advanced-sql.js
-```
-
-### Production Environment
-
-```bash
-# Create proper migration (one-time setup)
-npx prisma migrate dev --create-only --name advanced-sql-features
-# Copy content from sql/*.sql files to the migration
-npx prisma migrate deploy
-
-# Or use separate deployment script
-node sql/apply-advanced-sql.js
-```
-
-## 🔧 Current Implementation Status
-
-Based on your project setup:
-
-### ✅ What's Implemented:
-
-1. **Basic Indexes in Prisma Schema** (`schema.prisma`)
-
-   ```prisma
-   @@index([email])                    // Basic email index
-   @@index([email, tenantId])          // Composite index
-   @@index([status])                   // Status index
-   ```
-
-2. **Advanced SQL Features** (`sql/` directory)
-
-   - Complex invoice status trigger
-   - Advanced email indexes (partial, functional, composite)
-   - Notification system
-   - Status history tracking
-
-3. **Integrated Seeding** (`prisma/seed.js`)
-
-   - Applies both data seeding AND SQL features
-   - Non-blocking (continues if SQL fails)
-   - Perfect for development
-
-4. **Standalone Script** (`sql/apply-advanced-sql.js`)
-   - For production deployments
-   - Detailed verification and testing
-
-## 📝 Usage Instructions
-
-### For Development:
-
-```bash
-# Reset and setup everything
-npx prisma migrate reset
-npm run db:seed
-
-# This gives you:
-# ✅ Clean database
-# ✅ Initial data
-# ✅ Advanced SQL features
-# ✅ Verification output
-```
-
-### For Production:
-
-```bash
-# Method 1: Separate script
-npx prisma migrate deploy
-node sql/apply-advanced-sql.js
-
-# Method 2: Migration approach
-npx prisma migrate deploy  # (with SQL content in migration files)
-```
-
-## 🔍 Verification
-
-After running any approach, verify with:
-
-```sql
--- Check trigger exists
-SELECT tgname FROM pg_trigger WHERE tgname = 'invoice_status_update_trigger';
-
--- Check indexes exist
-SELECT indexname FROM pg_indexes
-WHERE tablename = 'Client' AND indexname LIKE 'idx_client_email%';
-
--- Test trigger
-UPDATE "Invoice" SET status = 'PAID' WHERE invoice_number = 'some-invoice';
-SELECT * FROM "InvoiceStatusHistory" ORDER BY changed_at DESC LIMIT 1;
-```
-
-## 🎓 Learning Outcomes
-
-This setup teaches:
-
-- **Prisma Limitations**: What can/cannot be done in schema.prisma
-- **Raw SQL Integration**: How to extend Prisma with custom SQL
-- **Deployment Strategies**: Different approaches for different environments
-- **Database Optimization**: Advanced indexing and trigger strategies
-
-## 💡 Recommendations
-
-1. **Use integrated seeding** during development
-2. **Create proper migrations** for production
-3. **Keep raw SQL in separate files** for maintainability
-4. **Document everything** (like this guide!)
-5. **Test thoroughly** in staging before production
-
-The current setup gives you flexibility to choose the best approach for your needs!
Index: l/README.md
===================================================================
--- sql/README.md	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ 	(revision )
@@ -1,227 +1,0 @@
-# Advanced SQL Implementation - Triggers & Indexing
-
-This directory contains advanced SQL implementations for the AgencyOS project, featuring complex triggers and performance-optimized indexes.
-
-## 🎯 Overview
-
-### Task 1: Complex Invoice Status Trigger
-
-- **File**: `01_invoice_status_trigger.sql`
-- **Purpose**: Implements a sophisticated trigger system that reacts to invoice status changes
-- **Complexity**: Multi-table operations, business logic automation, and notification system
-
-### Task 2: Client Email Index Optimization
-
-- **File**: `02_client_email_index.sql`
-- **Purpose**: Optimizes email lookup performance with multiple index strategies
-- **Optimization**: Multiple index types for different query patterns
-
-## 🚀 Implementation Guide
-
-### Prerequisites
-
-- PostgreSQL database (as configured in your Prisma setup)
-- Database connection with appropriate permissions
-- Prisma CLI installed
-
-### Step 1: Apply the SQL Files
-
-#### Option A: Using PostgreSQL CLI
-
-```bash
-# Connect to your database
-psql "your_database_connection_string"
-
-# Apply the trigger implementation
-\i sql/01_invoice_status_trigger.sql
-
-# Apply the index optimization
-\i sql/02_client_email_index.sql
-```
-
-#### Option B: Using Prisma Raw Queries
-
-```javascript
-// In a Node.js script or your application
-import { PrismaClient } from '@prisma/client';
-import fs from 'fs';
-
-const prisma = new PrismaClient();
-
-async function applySQLFiles() {
-  // Apply trigger SQL
-  const triggerSQL = fs.readFileSync('sql/01_invoice_status_trigger.sql', 'utf8');
-  await prisma.$executeRawUnsafe(triggerSQL);
-
-  // Apply index SQL
-  const indexSQL = fs.readFileSync('sql/02_client_email_index.sql', 'utf8');
-  await prisma.$executeRawUnsafe(indexSQL);
-
-  console.log('Advanced SQL features applied successfully!');
-}
-
-applySQLFiles();
-```
-
-#### Option C: Create Prisma Migration
-
-```bash
-# Create a new migration
-npx prisma migrate dev --create-only --name advanced-sql-features
-
-# Add the SQL content to the generated migration file
-# Then apply it
-npx prisma migrate dev
-```
-
-## 📋 Feature Details
-
-### Complex Invoice Status Trigger
-
-#### What it does:
-
-1. **Triggers on**: Invoice status updates (only when status actually changes)
-2. **Creates tables**:
-   - `InvoiceStatusHistory` - Logs all status changes
-   - `InvoiceNotifications` - Business rule notifications
-3. **Complex operations**:
-   - Calculates client outstanding amounts
-   - Monitors tenant monthly revenue
-   - Automatically updates client status
-   - Extends due dates for pending invoices
-   - Generates priority-based notifications
-
-#### Business Logic Examples:
-
-- **PAID invoices**: Updates client to ACTIVE if no outstanding invoices
-- **OVERDUE invoices**: Flags clients as INACTIVE if >3 overdue invoices
-- **PENDING invoices**: Warns about high outstanding amounts (>€10,000)
-- **Auto-extension**: Extends due dates for pending invoices with past due dates
-
-### Client Email Index Optimization
-
-#### Index Types Created:
-
-1. **B-tree Index** (`idx_client_email_btree`): For exact email matches
-2. **Partial Index** (`idx_client_email_active`): Only for active clients
-3. **Composite Index** (`idx_client_email_tenant`): Email + tenant_id optimization
-4. **Functional Index** (`idx_client_email_lower`): Case-insensitive searches
-
-#### Query Patterns Optimized:
-
-```sql
--- Exact match (uses idx_client_email_btree)
-SELECT * FROM "Client" WHERE email = 'test@example.com';
-
--- Case-insensitive (uses idx_client_email_lower)
-SELECT * FROM "Client" WHERE LOWER(email) = LOWER('Test@Example.com');
-
--- Multi-tenant (uses idx_client_email_tenant)
-SELECT * FROM "Client" WHERE email = 'test@example.com' AND tenant_id = 'tenant-123';
-
--- Active clients only (uses idx_client_email_active)
-SELECT * FROM "Client" WHERE email = 'test@example.com' AND status = 'ACTIVE';
-```
-
-## 🔧 Testing & Verification
-
-### Test the Trigger
-
-```sql
--- Update an invoice status to test the trigger
-UPDATE "Invoice"
-SET status = 'PAID'
-WHERE invoice_number = 'INV-001';
-
--- Check the results
-SELECT * FROM "InvoiceStatusHistory" ORDER BY changed_at DESC LIMIT 5;
-SELECT * FROM "InvoiceNotifications" ORDER BY created_at DESC LIMIT 5;
-```
-
-### Test the Indexes
-
-```sql
--- Check if indexes are being used
-EXPLAIN (ANALYZE, BUFFERS)
-SELECT * FROM "Client" WHERE email = 'test@example.com';
-
--- Should show "Index Scan using idx_client_email_btree"
-```
-
-### Performance Monitoring
-
-```sql
--- Check index usage statistics
-SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
-FROM pg_stat_user_indexes
-WHERE tablename = 'Client';
-```
-
-## 📊 Expected Performance Improvements
-
-### Before Optimization:
-
-- Email queries: Sequential scan (~100ms for 10k records)
-- No invoice status tracking
-- Manual business rule enforcement
-
-### After Optimization:
-
-- Email queries: Index scan (~1ms for 10k records)
-- Automatic invoice status tracking and business rules
-- Real-time notifications for business events
-- 99%+ performance improvement for email lookups
-
-## 🛠️ Maintenance
-
-### Monitor Trigger Performance
-
-```sql
--- Check trigger execution times
-SELECT * FROM "InvoiceStatusHistory"
-WHERE changed_at > NOW() - INTERVAL '1 day'
-ORDER BY changed_at DESC;
-```
-
-### Index Maintenance
-
-```sql
--- Rebuild indexes if needed (rarely required)
-REINDEX INDEX idx_client_email_btree;
-
--- Update statistics for query planner
-ANALYZE "Client";
-```
-
-## 🔍 Troubleshooting
-
-### Common Issues:
-
-1. **Trigger not firing**: Check if status actually changed
-2. **Index not used**: Run `ANALYZE "Client"` to update statistics
-3. **Performance issues**: Monitor with `pg_stat_statements`
-
-### Debug Queries:
-
-```sql
--- Check trigger exists
-SELECT tgname, tgrelid::regclass, tgenabled
-FROM pg_trigger
-WHERE tgname = 'invoice_status_update_trigger';
-
--- Check indexes exist
-SELECT indexname, indexdef
-FROM pg_indexes
-WHERE tablename = 'Client' AND indexname LIKE 'idx_client_email%';
-```
-
-## 🎓 Educational Value
-
-This implementation demonstrates:
-
-- **Trigger complexity**: Multi-table operations and business logic
-- **Index strategies**: Different index types for different use cases
-- **Performance optimization**: Query plan analysis and monitoring
-- **Real-world application**: Practical business rule automation
-
-The trigger goes beyond simple field updates to implement complex business rules, while the indexing strategy covers multiple query patterns for optimal performance.
Index: l/apply-advanced-sql.js
===================================================================
--- sql/apply-advanced-sql.js	(revision 9e4f0701afaf983c267afc9854e2a2699a10ff0d)
+++ 	(revision )
@@ -1,151 +1,0 @@
-#!/usr/bin/env node
-
-/**
- * Advanced SQL Features Application Script
- * 
- * This script applies the complex trigger and indexing optimizations
- * to your PostgreSQL database using Prisma's raw query capabilities.
- * 
- * Usage:
- *   node sql/apply-advanced-sql.js
- * 
- * Or add to package.json scripts:
- *   "apply-advanced-sql": "node sql/apply-advanced-sql.js"
- */
-
-const { PrismaClient } = require('@prisma/client');
-const fs = require('fs');
-const path = require('path');
-
-const prisma = new PrismaClient();
-
-async function applyAdvancedSQL() {
-  console.log('🚀 Applying Advanced SQL Features...\n');
-
-  try {
-    // Step 1: Apply Complex Invoice Status Trigger
-    console.log('📊 Step 1: Applying Complex Invoice Status Trigger...');
-    const triggerSQL = fs.readFileSync(
-      path.join(__dirname, '01_invoice_status_trigger.sql'),
-      'utf8'
-    );
-    
-    await prisma.$executeRawUnsafe(triggerSQL);
-    console.log('✅ Invoice status trigger applied successfully!');
-    
-    // Verify trigger creation
-    const triggerCheck = await prisma.$queryRaw`
-      SELECT tgname, tgrelid::regclass as table_name, tgenabled 
-      FROM pg_trigger 
-      WHERE tgname = 'invoice_status_update_trigger'
-    `;
-    
-    if (triggerCheck.length > 0) {
-      console.log('✅ Trigger verification: invoice_status_update_trigger is active');
-    } else {
-      console.log('⚠️  Warning: Trigger not found in verification check');
-    }
-
-    // Step 2: Apply Client Email Index Optimization
-    console.log('\n📈 Step 2: Applying Client Email Index Optimization...');
-    const indexSQL = fs.readFileSync(
-      path.join(__dirname, '02_client_email_index.sql'),
-      'utf8'
-    );
-    
-    await prisma.$executeRawUnsafe(indexSQL);
-    console.log('✅ Email index optimization applied successfully!');
-    
-    // Verify index creation
-    const indexCheck = await prisma.$queryRaw`
-      SELECT indexname, indexdef 
-      FROM pg_indexes 
-      WHERE tablename = 'Client' AND indexname LIKE 'idx_client_email%'
-      ORDER BY indexname
-    `;
-    
-    console.log(`✅ Index verification: ${indexCheck.length} email indexes created:`);
-    indexCheck.forEach(index => {
-      console.log(`   - ${index.indexname}`);
-    });
-
-    // Step 3: Test the implementations
-    console.log('\n🧪 Step 3: Running basic tests...');
-    
-    // Test trigger tables exist
-    const historyTableExists = await prisma.$queryRaw`
-      SELECT EXISTS (
-        SELECT FROM information_schema.tables 
-        WHERE table_name = 'InvoiceStatusHistory'
-      )
-    `;
-    
-    const notificationsTableExists = await prisma.$queryRaw`
-      SELECT EXISTS (
-        SELECT FROM information_schema.tables 
-        WHERE table_name = 'InvoiceNotifications'
-      )
-    `;
-    
-    console.log(`✅ InvoiceStatusHistory table exists: ${historyTableExists[0].exists}`);
-    console.log(`✅ InvoiceNotifications table exists: ${notificationsTableExists[0].exists}`);
-    
-    // Test email query performance (if Client table has data)
-    const clientCount = await prisma.client.count();
-    if (clientCount > 0) {
-      console.log(`✅ Client table has ${clientCount} records - indexes ready for optimization`);
-      
-      // Show query plan for email lookup
-      const queryPlan = await prisma.$queryRaw`
-        EXPLAIN (FORMAT JSON) 
-        SELECT * FROM "Client" WHERE email = 'test@example.com'
-      `;
-      
-      const planText = JSON.stringify(queryPlan[0], null, 2);
-      if (planText.includes('Index Scan')) {
-        console.log('✅ Email queries are using index optimization');
-      } else {
-        console.log('ℹ️  Email queries will use indexes when data grows larger');
-      }
-    } else {
-      console.log('ℹ️  Client table is empty - indexes ready for when data is added');
-    }
-
-    console.log('\n🎉 Advanced SQL Features Applied Successfully!');
-    console.log('\n📋 What was implemented:');
-    console.log('   • Complex invoice status trigger with business logic');
-    console.log('   • Multiple email index strategies for optimal performance');
-    console.log('   • Invoice status history tracking');
-    console.log('   • Automated business rule notifications');
-    console.log('   • Performance monitoring capabilities');
-    
-    console.log('\n🔍 Next Steps:');
-    console.log('   • Check sql/README.md for detailed documentation');
-    console.log('   • Update invoice statuses to test the trigger');
-    console.log('   • Monitor query performance with EXPLAIN ANALYZE');
-    console.log('   • Review InvoiceStatusHistory and InvoiceNotifications tables');
-
-  } catch (error) {
-    console.error('❌ Error applying advanced SQL features:');
-    console.error(error.message);
-    
-    if (error.message.includes('permission')) {
-      console.log('\n💡 Tip: Make sure your database user has sufficient privileges:');
-      console.log('   • CREATE TABLE');
-      console.log('   • CREATE INDEX');
-      console.log('   • CREATE FUNCTION');
-      console.log('   • CREATE TRIGGER');
-    }
-    
-    process.exit(1);
-  } finally {
-    await prisma.$disconnect();
-  }
-}
-
-// Run the script
-if (require.main === module) {
-  applyAdvancedSQL();
-}
-
-module.exports = { applyAdvancedSQL }; 
