"""
Test script to verify SIMATIC SD compatibility detection
"""

import os
import sys

# --- TIA Scripting Import Handling ---
if os.getenv("TIA_SCRIPTING"):
    sys.path.append(os.getenv("TIA_SCRIPTING"))

try:
    import siemens_tia_scripting as ts

    print("✓ TIA Scripting import successful")
    print(
        f"Available programming languages: {[lang for lang in dir(ts.Enums.ProgrammingLanguage) if not lang.startswith('_')]}"
    )
    print(
        f"Available export formats: {[fmt for fmt in dir(ts.Enums.ExportFormats) if not fmt.startswith('_')]}"
    )
    print(
        f"Available block types: {[bt for bt in dir(ts.Enums.BlockType) if not bt.startswith('_')]}"
    )

    # Check if SIMATIC SD is available
    try:
        simatic_sd_format = ts.Enums.ExportFormats.SimaticSD
        print(f"✓ SIMATIC SD format available: {simatic_sd_format}")
    except AttributeError:
        print("✗ SIMATIC SD format NOT available in this TIA Scripting version")

except ImportError as e:
    print(f"✗ Failed to import TIA Scripting: {e}")
    print(
        "This is expected if TIA Portal is not installed or TIA_SCRIPTING env var not set"
    )


def analyze_simatic_sd_requirements():
    """Analyze and display SIMATIC SD requirements"""
    print("\n=== SIMATIC SD FORMAT REQUIREMENTS ===")
    print("Based on official Siemens documentation:")
    print()
    print("✓ SUPPORTED:")
    print("  • Programming Language: LAD (Ladder) ONLY")
    print(
        "  • Block Types: FB (Function Block), FC (Function), OB (Organization Block)"
    )
    print("  • TIA Portal Version: V20 or later")
    print("  • Target PLCs: S7-1200, S7-1500")
    print()
    print("✗ NOT SUPPORTED:")
    print("  • SCL (Structured Control Language)")
    print("  • STL (Statement List)")
    print("  • FBD (Function Block Diagram)")
    print("  • Graph programming")
    print("  • CFC (Continuous Function Chart)")
    print("  • Complex LAD elements (some advanced functions)")
    print()
    print("📋 COMMON CAUSES FOR XML-ONLY EXPORT:")
    print("  1. Block programmed in SCL/STL/FBD instead of LAD")
    print("  2. Block contains unsupported LAD elements")
    print("  3. Block is not compiled/consistent")
    print("  4. TIA Portal version < V20")
    print("  5. Wrong block type (not FB/FC/OB)")


if __name__ == "__main__":
    analyze_simatic_sd_requirements()
