# Get all pay providers from payments folder
# Providers must have PayProvider class and is_available method
import os
import importlib
from typing import Dict
from payments.pay_provider import BasePayProvider

providers: Dict[str, BasePayProvider] = {}

async def get_available_providers(bot_username: str) -> Dict[str, BasePayProvider]:
    global providers
    # Get all python files from payments folder
    payments_dir = os.path.join(os.path.dirname(__file__), '..', 'payments')
    for filename in os.listdir(payments_dir):
        if filename.endswith('.py') and not filename.startswith('__'):
            # Import module dynamically
            module_name = filename[:-3]  # Remove .py extension
            try:
                module = importlib.import_module(f'payments.{module_name}')
                # Check if module has PayProvider class
                if hasattr(module, 'PayProvider'):
                    provider = module.PayProvider(bot_username)
                    # Check if provider is available
                    if await provider.is_available():
                        providers[provider.callback_data_prefix] = provider
            except Exception as e:
                print(f"Error loading payment provider {module_name}: {e}")
    print("Providers loaded: ", providers)
    return providers