#!/usr/bin/env python3
"""
Verify if chart recognition can be enabled in the current PaddlePaddle version
Run this in the conda environment: conda activate tool_ocr && python verify_chart_recognition.py
"""

import sys

def check_paddle_api():
    """Check if fused_rms_norm_ext API is available"""
    try:
        import paddle
        print(f"✅ PaddlePaddle version: {paddle.__version__}")

        # Check if the API exists
        import paddle.incubate.nn.functional as F

        has_base = hasattr(F, 'fused_rms_norm')
        has_ext = hasattr(F, 'fused_rms_norm_ext')

        print(f"\n📊 API Availability:")
        print(f"   - fused_rms_norm:     {'✅ Available' if has_base else '❌ Not found'}")
        print(f"   - fused_rms_norm_ext: {'✅ Available' if has_ext else '❌ Not found'}")

        if has_ext:
            print(f"\n🎉 Chart recognition CAN be enabled!")
            print(f"\n📝 Action required:")
            print(f"   1. Edit backend/app/services/ocr_service.py")
            print(f"   2. Change line 217: use_chart_recognition=False → True")
            print(f"   3. Restart the backend service")
            print(f"\n⚠️  Note: This will enable deep chart analysis (may increase processing time)")
            return True
        else:
            print(f"\n❌ Chart recognition CANNOT be enabled yet")
            print(f"\n📝 Current PaddlePaddle version ({paddle.__version__}) does not support fused_rms_norm_ext")
            print(f"\n💡 Options:")
            print(f"   1. Upgrade PaddlePaddle: pip install --upgrade paddlepaddle>=3.2.0")
            print(f"   2. Check for newer versions: pip search paddlepaddle")
            print(f"   3. Wait for official PaddlePaddle update")
            return False

    except ImportError as e:
        print(f"❌ PaddlePaddle not installed: {e}")
        print(f"\n💡 Install PaddlePaddle:")
        print(f"   pip install paddlepaddle>=3.2.0")
        return False
    except Exception as e:
        print(f"❌ Error: {e}")
        return False

if __name__ == "__main__":
    print("=" * 70)
    print("Chart Recognition Availability Checker")
    print("=" * 70)
    print()

    can_enable = check_paddle_api()

    print()
    print("=" * 70)
    sys.exit(0 if can_enable else 1)
