#!/usr/bin/env python3
"""
#exonware/xwsystem/tests/3.advance/runner.py

Advance test runner for xwsystem - Excellence Validation
Tests against eXonware's 5 core priorities.

OPTIONAL until v1.0.0, MANDATORY for production releases.

Company: eXonware.com
Author: Eng. Muhammad AlShehri
Email: connect@exonware.com
Version: 0.0.1.388
Generation Date: 01-Nov-2025

Usage:
    python tests/3.advance/runner.py  # Run all advance tests
    python tests/3.advance/runner.py --security  # Priority #1
    python tests/3.advance/runner.py --performance  # Priority #4
    python tests/3.advance/runner.py --extensibility  # Priority #5
"""

import sys
import pytest
from pathlib import Path
from datetime import datetime

# Add src to path
src_path = Path(__file__).parent.parent.parent / "src"
sys.path.insert(0, str(src_path))


def main():
    """Advance test runner - Excellence validation."""
    print("🎓 Advance Tests - Excellence Validation")
    print("   Testing against eXonware's 5 Core Priorities")
    
    # Parse arguments
    args = sys.argv[1:]
    advance_dir = Path(__file__).parent
    
    # Determine which tests to run
    category = "all"
    if "--security" in args:
        category = "security"
        print("   Priority #1: Security Excellence")
        exit_code = pytest.main([
            "-v",
            "--tb=short",
            "-x",
            str(advance_dir),
            "-m", "xsystem_security",
            "-k", "caching"  # Only caching-related security tests
        ])
    
    elif "--performance" in args:
        category = "performance"
        print("   Priority #4: Performance Excellence")
        exit_code = pytest.main([
            "-v",
            "--tb=short",
            "-x",
            str(advance_dir),
            "-m", "xsystem_performance",
            "-k", "caching"
        ])
    
    elif "--extensibility" in args:
        category = "extensibility"
        print("   Priority #5: Extensibility Excellence")
        exit_code = pytest.main([
            "-v",
            "--tb=short",
            "-x",
            str(advance_dir),
            "-m", "xsystem_extensibility",
            "-k", "caching"
        ])
    
    else:
        # Run all caching advance tests
        print("   All Priorities for Caching Module")
        exit_code = pytest.main([
            "-v",
            "--tb=short",
            "-x",
            str(advance_dir),
            "-m", "xsystem_advance",
            "-k", "caching"
        ])
    
    # Status
    if exit_code == 0:
        status = "✅ Advance tests PASSED"
    else:
        status = "❌ Advance tests FAILED"
    
    print(f"\n{status}")
    
    # Save Markdown output
    output_file = advance_dir / "runner_out.md"
    priorities_map = {
        "security": "Priority #1: Security Excellence",
        "performance": "Priority #4: Performance Excellence",
        "extensibility": "Priority #5: Extensibility Excellence",
        "all": "All Priorities (Caching Module)"
    }
    
    markdown_content = f"""# Advance Test Results - Caching Module

**Layer:** 3.advance
**Generated:** {datetime.now().strftime("%d-%b-%Y %H:%M:%S")}
**Category:** {priorities_map.get(category, category)}
**Status:** {'PASSED' if exit_code == 0 else 'FAILED'}

---

## eXonware's 5 Core Priorities

| Priority | Category | Test File |
|----------|----------|-----------|
| #1 | **Security** | `test_caching_security.py` |
| #4 | **Performance** | `test_caching_performance.py` |
| #5 | **Extensibility** | `test_caching_extensibility.py` |

---

## Test Execution

**Category Tested:** {priorities_map.get(category, category)}

**Marker:** `xsystem_{category if category != 'all' else 'advance'}` + caching filter

---

## Result

{status}

**Exit Code:** {exit_code}

---

### Production Readiness

{'✅ Caching module meets excellence standards for the tested category.' if exit_code == 0 else '❌ Caching module requires improvements before production deployment.'}

---

*Generated by eXonware test runner - Excellence Edition*
"""
    
    output_file.write_text(markdown_content, encoding='utf-8')
    print(f"\n📝 Results saved to: {output_file}")
    
    sys.exit(exit_code)


if __name__ == "__main__":
    main()

