"""
获取数据库中的意图规则
"""

import asyncio
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from app.models.entities import IntentRule
from app.core.config import get_settings


async def get_intent_rules():
    """获取所有意图规则"""
    settings = get_settings()
    engine = create_async_engine(settings.database_url)
    async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
    
    async with async_session() as session:
        result = await session.execute(
            select(IntentRule).where(
                IntentRule.tenant_id == "szmp@ash@2026",
                IntentRule.is_enabled == True
            )
        )
        rules = result.scalars().all()
        
        print("=" * 80)
        print("数据库中的意图规则 (tenant=szmp@ash@2026):")
        print("=" * 80)
        
        if not rules:
            print("\n没有找到任何启用的意图规则！")
        else:
            for rule in rules:
                print(f"\n规则: {rule.name}")
                print(f"  ID: {rule.id}")
                print(f"  响应类型: {rule.response_type}")
                print(f"  关键词: {rule.keywords}")
                print(f"  正则模式: {rule.patterns}")
                print(f"  目标知识库: {rule.target_kb_ids}")
                print(f"  优先级: {rule.priority}")
                print(f"  启用: {rule.is_enabled}")
        
        print("\n" + "=" * 80)


if __name__ == "__main__":
    asyncio.run(get_intent_rules())
