"""
GitLab CI/CD templates and configurations for Storm Logger projects
"""

import json
from typing import Dict, Any, List


def get_gitlab_enterprise_templates() -> str:
    """
    GitLab enterprise CI/CD templates and configurations
    
    Returns:
        JSON string with GitLab enterprise templates
    """
    templates_data = {
        "title": "GitLab Enterprise CI/CD Templates",
        "description": "Production-ready GitLab CI/CD configurations with Storm Logger integration",
        "storm_logger_integration": {
            "benefits": [
                "Comprehensive build and test logging",
                "Business intelligence for CI/CD metrics",
                "Professional console output for GitLab runners",
                "Automated performance tracking",
                "Security event logging and compliance"
            ],
            "setup": {
                "requirements": [
                    "Add storm-logger to requirements.txt",
                    "Configure environment variables",
                    "Set up proper permissions"
                ],
                "environment_variables": {
                    "STORM_LOGGER_LEVEL": "INFO",
                    "STORM_ENABLE_BUSINESS_METRICS": "true",
                    "STORM_CONSOLE_THEME": "professional"
                }
            }
        },
        "basic_pipeline": {
            "description": "Basic GitLab CI/CD pipeline with Storm Logger",
            "filename": ".gitlab-ci.yml",
            "content": {
                "stages": ["build", "test", "deploy"],
                "variables": {
                    "STORM_LOGGER_LEVEL": "INFO",
                    "STORM_ENABLE_BUSINESS_METRICS": "true"
                },
                "before_script": [
                    "pip install storm-logger",
                    "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-ci'); logger.info('Pipeline started')\""
                ],
                "build": {
                    "stage": "build",
                    "script": [
                        "echo 'Building application...'",
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-build'); logger.info('Build completed')\""
                    ]
                },
                "test": {
                    "stage": "test",
                    "script": [
                        "echo 'Running tests...'",
                        "pytest",
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-test'); logger.track_business_event('test_completion', {'success': True})\""
                    ]
                },
                "deploy": {
                    "stage": "deploy",
                    "script": [
                        "echo 'Deploying application...'",
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-deploy'); logger.track_business_event('deployment', {'environment': 'production', 'status': 'success'})\""
                    ],
                    "when": "manual"
                }
            }
        },
        "enterprise_pipeline": {
            "description": "Enterprise-grade GitLab CI/CD pipeline with comprehensive features",
            "filename": ".gitlab-ci.yml",
            "features": [
                "Multi-stage deployment",
                "Parallel testing",
                "Security scanning",
                "Performance monitoring",
                "Compliance reporting"
            ],
            "content": {
                "stages": ["build", "test", "security", "deploy", "monitor"],
                "variables": {
                    "STORM_LOGGER_LEVEL": "INFO",
                    "STORM_ENABLE_BUSINESS_METRICS": "true",
                    "STORM_CONSOLE_THEME": "professional",
                    "STORM_ENABLE_SECURITY_METRICS": "true"
                },
                "build": {
                    "stage": "build",
                    "parallel": {
                        "matrix": [
                            {"PYTHON_VERSION": "3.10"},
                            {"PYTHON_VERSION": "3.11"},
                            {"PYTHON_VERSION": "3.12"}
                        ]
                    },
                    "script": [
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-build'); logger.info(f'Building with Python {os.environ.get(\"PYTHON_VERSION\")}')\"",
                        "python setup.py build"
                    ]
                },
                "test": {
                    "stage": "test",
                    "parallel": 3,
                    "script": [
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-test'); logger.info('Starting parallel test execution')\"",
                        "pytest --parallel-safe",
                        "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-test'); logger.track_business_event('test_completion', {'parallel': True})\""
                    ]
                }
            }
        },
        "python_specific": {
            "description": "Python-specific GitLab CI/CD templates",
            "matrix_testing": {
                "python_versions": ["3.10", "3.11", "3.12"],
                "dependencies": ["minimal", "full", "dev"]
            },
            "quality_gates": {
                "formatting": "black --check .",
                "linting": "ruff check .",
                "type_checking": "mypy .",
                "security": "bandit -r .",
                "coverage": "pytest --cov=. --cov-report=xml"
            }
        },
        "deployment_strategies": {
            "rolling_deployment": {
                "description": "Rolling deployment with zero downtime",
                "environments": ["staging", "production"],
                "health_checks": True
            },
            "blue_green_deployment": {
                "description": "Blue-green deployment strategy",
                "environments": ["blue", "green"],
                "traffic_switching": True
            },
            "canary_deployment": {
                "description": "Canary deployment with gradual rollout",
                "traffic_percentage": [10, 25, 50, 100],
                "monitoring": True
            }
        },
        "monitoring_integration": {
            "storm_logger_metrics": [
                "Build success/failure rates",
                "Test execution times",
                "Deployment frequency",
                "Pipeline performance metrics"
            ],
            "external_tools": [
                "Monitor pipeline performance with Storm Logger",
                "Track business metrics across deployments",
                "Generate compliance reports",
                "Alert on performance degradation"
            ]
        }
    }
    
    return json.dumps(templates_data, indent=2)


def get_pipeline_templates() -> Dict[str, Any]:
    """
    Get GitLab pipeline templates
    
    Returns:
        Dictionary with various pipeline templates
    """
    return {
        "basic_python": {
            "stages": ["build", "test", "deploy"],
            "variables": {
                "STORM_LOGGER_LEVEL": "INFO",
                "STORM_ENABLE_BUSINESS_METRICS": "true"
            },
            "before_script": [
                "pip install storm-logger",
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-ci'); logger.info('Pipeline started')\""
            ]
        },
        "enterprise_python": {
            "stages": ["build", "test", "security", "deploy", "monitor"],
            "variables": {
                "STORM_LOGGER_LEVEL": "INFO",
                "STORM_ENABLE_BUSINESS_METRICS": "true",
                "STORM_CONSOLE_THEME": "professional",
                "STORM_ENABLE_SECURITY_METRICS": "true"
            },
            "parallel_testing": True,
            "security_scanning": True,
            "performance_monitoring": True
        },
        "multi_environment": {
            "stages": ["build", "test", "deploy_staging", "deploy_production"],
            "environments": {
                "staging": {
                    "url": "https://staging.example.com",
                    "on_stop": "stop_staging"
                },
                "production": {
                    "url": "https://production.example.com",
                    "when": "manual"
                }
            }
        }
    }


def get_storm_integration_snippets() -> Dict[str, Any]:
    """
    Get Storm Logger integration snippets for GitLab CI/CD
    
    Returns:
        Dictionary with integration code snippets
    """
    return {
        "initialization": {
            "before_script": [
                "pip install storm-logger",
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-ci'); logger.info('Pipeline started')\""
            ]
        },
        "build_logging": {
            "script": [
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-build'); logger.info('Build started')\"",
                "# Your build commands here",
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-build'); logger.info('Build completed')\""
            ]
        },
        "test_tracking": {
            "script": [
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-test'); logger.info('Test execution started')\"",
                "pytest",
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-test'); logger.track_business_event('test_completion', {'success': True})\""
            ]
        },
        "deployment_tracking": {
            "script": [
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-deploy'); logger.track_business_event('deployment_start', {'environment': 'production'})\"",
                "# Your deployment commands here",
                "python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-deploy'); logger.track_business_event('deployment_success', {'environment': 'production'})\""
            ]
        },
        "error_handling": {
            "after_script": [
                "if [ \"$CI_JOB_STATUS\" == \"failed\" ]; then",
                "  python -c \"from storm_logger import get_storm_logger; logger = get_storm_logger('gitlab-error'); logger.error('Pipeline failed at stage: $CI_JOB_STAGE')\"",
                "fi"
            ]
        }
    }