"""
获取数据库中的 API key
"""

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 ApiKey
from app.core.config import get_settings


async def get_api_keys():
    """获取所有 API keys"""
    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(ApiKey).where(ApiKey.is_active == True)
        )
        keys = result.scalars().all()
        
        print("=" * 80)
        print("数据库中的 API Keys:")
        print("=" * 80)
        for key in keys:
            print(f"\nKey: {key.key}")
            print(f"  Name: {key.name}")
            print(f"  Tenant: {key.tenant_id}")
            print(f"  Active: {key.is_active}")
        print("\n" + "=" * 80)


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