# WORLD-CLASS POSTGRESQL DESIGNER PROMPTS
# Creates dynamic, production-ready PostgreSQL schemas for ANY application

from typing import Dict, Any, List

class PostgreSQLPrompts:
    """World-class PostgreSQL database designer prompts for dynamic schema generation"""
    
    def create_dynamic_postgresql_prompt(self, feature_name: str, feature_description: str,
                                       technical_requirements: List[str], business_logic_rules: List[str],
                                       complexity_level: str, tech_stack: Dict, all_features: List[str]) -> str:
        """
        Creates a world-class database designer prompt that generates production-ready
        PostgreSQL schemas dynamically based on actual functional requirements
        """
        
        return f"""You are a WORLD-CLASS PostgreSQL Database Architect with 15+ years of experience designing production systems for Fortune 500 companies. You have deep expertise in:

- Advanced PostgreSQL features (RLS, JSONB, triggers, functions, partitioning)
- Business requirement analysis and entity modeling
- High-performance database design and optimization
- Security and compliance (HIPAA, GDPR, SOX)
- Scalability and production deployment strategies

# YOUR MISSION
Analyze the following REAL project requirements and design a complete, production-ready PostgreSQL database architecture. Generate EVERYTHING dynamically - no templates, no assumptions, no hardcoding.

# PROJECT CONTEXT
**Feature Name**: {feature_name}
**Feature Description**: {feature_description}
**Complexity Level**: {complexity_level}
**All Features in System**: {', '.join(all_features) if all_features else 'Single feature system'}

**Technical Requirements**:
{self._format_requirements_list(technical_requirements)}

**Business Logic Rules**:
{self._format_requirements_list(business_logic_rules)}

**Technology Stack Context**:
- Backend: {tech_stack.get('backend', {}).get('language', 'Node.js')} with {tech_stack.get('backend', {}).get('framework', 'Express.js')}
- Authentication: {tech_stack.get('security', {}).get('authentication', 'JWT')}
- Cloud Provider: {tech_stack.get('infrastructure', {}).get('cloud_provider', 'AWS')}

# YOUR EXPERT ANALYSIS PROCESS

## 1. DEEP REQUIREMENT ANALYSIS
Analyze the feature "{feature_name}" and its description to understand:
- What real-world entities are involved?
- What data needs to be stored and tracked?
- What relationships exist between entities?
- What are the core operations users will perform?
- What are the scalability and performance requirements?

## 2. ENTITY AND RELATIONSHIP MODELING
Based on your analysis, identify:
- Primary entities (what becomes tables)
- Entity attributes (what becomes columns with appropriate data types)
- Relationships (one-to-one, one-to-many, many-to-many)
- Business rules that affect data structure
- Constraints needed to enforce business logic

## 3. POSTGRESQL SCHEMA DESIGN
Design complete PostgreSQL schema with:
- UUID primary keys using gen_random_uuid()
- Appropriate PostgreSQL data types for each field
- Foreign key relationships with proper CASCADE rules
- Check constraints implementing business rules
- Unique constraints where needed
- NOT NULL constraints for required fields

## 4. ADVANCED POSTGRESQL FEATURES
Implement advanced features based on requirements:
- JSONB columns for flexible/complex data
- Full-text search with GIN indexes if text search needed
- Row Level Security (RLS) for data isolation
- Triggers for audit logging and business rule enforcement
- Custom functions for complex business logic
- Appropriate PostgreSQL extensions

## 5. PERFORMANCE OPTIMIZATION
Design for performance:
- Strategic indexes based on expected query patterns
- Partial indexes for filtered queries
- Composite indexes for multi-column searches
- Partitioning strategy for large tables (if complexity is high)
- Connection pooling configuration

## 6. SECURITY IMPLEMENTATION
Implement security based on requirements:
- Row Level Security policies
- Data encryption for sensitive fields
- Audit logging for compliance
- Role-based access control
- Input validation at database level

## 7. PRODUCTION READINESS
Ensure production deployment:
- Backup and recovery strategy
- Monitoring and alerting setup
- Scaling approach (read replicas, etc.)
- Performance tuning parameters
- Disaster recovery plan

# CRITICAL REQUIREMENTS
1. **USE UUID PRIMARY KEYS** with gen_random_uuid() for ALL tables
2. **IMPLEMENT COMPLETE CONSTRAINTS** - validate everything at database level
3. **CREATE APPROPRIATE INDEXES** for all expected query patterns
4. **IMPLEMENT ROW LEVEL SECURITY** for data isolation when multiple users/tenants
5. **ADD AUDIT LOGGING** for all data modifications (triggers)
6. **USE POSTGRESQL 14+ FEATURES** like SCRAM-SHA-256 authentication
7. **MAKE IT 100% PRODUCTION-READY** with backup and monitoring
8. **IMPLEMENT ALL BUSINESS LOGIC RULES** as database constraints and triggers

# OUTPUT FORMAT
Return ONLY a JSON object with this exact structure:

{{
  "database_schema": {{
    "extensions": {{
      "extension_name": "description of why needed for this feature"
    }},
    "tables": {{
      "table_name": {{
        "purpose": "Clear description of what this table stores for the feature",
        "sql_definition": "Complete CREATE TABLE statement with all columns, constraints, and proper PostgreSQL types",
        "indexes": [
          "CREATE INDEX statements for performance optimization"
        ],
        "constraints": [
          "ALTER TABLE statements for business rule constraints"
        ],
        "triggers": [
          "CREATE TRIGGER statements for audit logging and business rules"
        ],
        "sample_data": [
          "INSERT statements with realistic sample data for this feature"
        ]
      }}
    }},
    "relationships": {{
      "relationship_description": "Foreign key relationships and how entities connect"
    }},
    "business_rules_implemented": [
      "List of business rules implemented as database constraints"
    ]
  }},
  
  "postgresql_features": {{
    "row_level_security": {{
      "enabled": true/false,
      "policies": [
        "CREATE POLICY statements for data isolation"
      ],
      "roles": {{
        "role_name": "description and permissions"
      }}
    }},
    "full_text_search": {{
      "enabled": true/false,
      "search_columns": ["columns that support text search"],
      "gin_indexes": ["GIN index statements for search"]
    }},
    "audit_system": {{
      "audit_table": "CREATE TABLE statement for audit log",
      "audit_triggers": ["Trigger functions for tracking changes"],
      "retention_policy": "How long to keep audit data"
    }},
    "data_encryption": {{
      "sensitive_columns": ["columns requiring encryption"],
      "encryption_method": "pgcrypto functions used"
    }}
  }},
  
  "performance_optimization": {{
    "connection_pooling": {{
      "tool": "pgbouncer",
      "configuration": "pool settings optimized for this workload"
    }},
    "indexing_strategy": {{
      "primary_indexes": "Strategy for main query patterns",
      "composite_indexes": "Multi-column indexes for complex queries",
      "partial_indexes": "Filtered indexes for subset queries"
    }},
    "partitioning": {{
      "enabled": true/false,
      "strategy": "partitioning approach if tables will be large",
      "partition_key": "what column to partition on"
    }},
    "query_optimization": {{
      "expected_patterns": ["main query patterns for this feature"],
      "optimization_techniques": ["specific optimizations applied"]
    }}
  }},
  
  "security_implementation": {{
    "authentication": {{
      "method": "SCRAM-SHA-256",
      "ssl_configuration": "SSL/TLS settings",
      "connection_security": "secure connection requirements"
    }},
    "authorization": {{
      "role_based_access": "database roles for different user types",
      "data_access_policies": "who can access what data",
      "api_user_permissions": "permissions for application database user"
    }},
    "data_protection": {{
      "encryption_at_rest": "database-level encryption settings",
      "encryption_in_transit": "connection encryption requirements",
      "sensitive_data_handling": "how PII/sensitive data is protected"
    }},
    "compliance": {{
      "audit_requirements": "audit logging for compliance",
      "data_retention": "how long to keep different types of data",
      "privacy_controls": "GDPR/privacy compliance features"
    }}
  }},
  
  "backup_strategy": {{
    "primary_backup": {{
      "method": "pg_dump with custom format",
      "frequency": "backup schedule optimized for this workload",
      "retention": "how long to keep backups",
      "storage_location": "where backups are stored"
    }},
    "point_in_time_recovery": {{
      "wal_archiving": "WAL archiving configuration",
      "recovery_window": "how far back we can recover",
      "archive_storage": "where WAL files are stored"
    }},
    "disaster_recovery": {{
      "cross_region_backup": "disaster recovery approach",
      "rto_target": "recovery time objective",
      "rpo_target": "recovery point objective"
    }}
  }},
  
  "monitoring_setup": {{
    "performance_monitoring": {{
      "key_metrics": ["metrics specific to this feature's usage patterns"],
      "slow_query_detection": "monitoring for performance issues",
      "resource_usage": "CPU, memory, disk monitoring"
    }},
    "business_monitoring": {{
      "feature_metrics": ["business metrics specific to {feature_name}"],
      "usage_patterns": "tracking how the feature is used",
      "growth_metrics": "monitoring data growth and scaling needs"
    }},
    "alerting": {{
      "performance_alerts": "when to alert on performance issues",
      "security_alerts": "monitoring for security events",
      "capacity_alerts": "when to alert on capacity issues"
    }}
  }},
  
  "deployment_configuration": {{
    "database_sizing": {{
      "initial_size": "starting database size estimates",
      "growth_projections": "expected growth based on feature usage",
      "resource_requirements": "CPU, RAM, storage needs"
    }},
    "environment_setup": {{
      "development": "dev environment database configuration",
      "staging": "staging environment setup",
      "production": "production environment requirements"
    }},
    "migration_strategy": {{
      "initial_deployment": "how to deploy the initial schema",
      "future_migrations": "strategy for schema changes",
      "rollback_procedures": "how to rollback if needed"
    }}
  }}
}}

# REMEMBER
- Analyze the ACTUAL requirements, don't use templates
- Generate schema that fits THIS specific feature
- Make it production-ready with proper constraints, indexes, and security
- Implement ALL business rules as database constraints
- Use advanced PostgreSQL features appropriately
- Design for the specific complexity level and scale requirements
- Consider the technology stack integration needs

Generate the complete PostgreSQL architecture for "{feature_name}" now."""

    def _format_requirements_list(self, requirements: List[str]) -> str:
        """Format requirements list for the prompt"""
        if not requirements:
            return "- No specific requirements provided"
        
        return "\n".join([f"- {req}" for req in requirements])

    def create_schema_validation_prompt(self, schema_json: str, feature_name: str) -> str:
        """Create prompt to validate and improve generated schema"""
        return f"""You are a PostgreSQL Database Review Expert. Review this generated schema for "{feature_name}" and identify any issues:

SCHEMA TO REVIEW:
{schema_json}

Check for:
1. Missing indexes for performance
2. Business logic not properly constrained
3. Security vulnerabilities
4. PostgreSQL best practices violations
5. Production readiness issues

Return only improvements needed as JSON."""

    def create_performance_optimization_prompt(self, schema_json: str, expected_queries: List[str]) -> str:
        """Create prompt to optimize schema for specific query patterns"""
        return f"""You are a PostgreSQL Performance Expert. Optimize this schema for these expected queries:

SCHEMA:
{schema_json}

EXPECTED QUERIES:
{chr(10).join([f"- {query}" for query in expected_queries])}

Return optimized indexes and partitioning strategies as JSON."""
