#!/usr/bin/env python3
"""
Test disponibilidad de funciones Everything3
"""

import ctypes
import os
import sys
from pathlib import Path

# Agregar el directorio src al path
current_dir = Path(__file__).parent
src_dir = current_dir / "src"
sys.path.insert(0, str(src_dir))


def test_everything3_functions():
    """Test which Everything3 functions are available"""

    dll_path = "Everything-SDK3/dll/Everything3_x64.dll"

    if not os.path.exists(dll_path):
        print(f"❌ DLL no encontrada: {dll_path}")
        return

    print(f"✅ DLL encontrada: {dll_path}")

    try:
        dll = ctypes.WinDLL(dll_path)
        print("✅ DLL cargada exitosamente")

        # Lista de funciones a probar
        functions_to_test = [
            "Everything3_ConnectW",
            "Everything3_Disconnect",
            "Everything3_IsDBLoaded",
            "Everything3_Search",
            "Everything3_GetResults",
            "Everything3_GetResultPropertyValueAsString",
            "Everything3_GetLastError",
            "Everything3_SetSearchTextW",
            "Everything3_QueryW",
            "Everything3_GetNumResults",
            "Everything3_GetResultFullPathNameW",
        ]

        available_functions = []
        missing_functions = []

        for func_name in functions_to_test:
            try:
                func = getattr(dll, func_name)
                available_functions.append(func_name)
                print(f"✅ {func_name} - Disponible")
            except AttributeError:
                missing_functions.append(func_name)
                print(f"❌ {func_name} - No disponible")

        print(f"\n📊 Resumen:")
        print(f"✅ Funciones disponibles: {len(available_functions)}")
        print(f"❌ Funciones faltantes: {len(missing_functions)}")

        if missing_functions:
            print(f"\n🚫 Funciones faltantes:")
            for func in missing_functions:
                print(f"  - {func}")

        # Intentar una conexión básica si está disponible
        if "Everything3_ConnectW" in available_functions:
            print(f"\n🔍 Probando conexión básica...")
            try:
                dll.Everything3_ConnectW.argtypes = [ctypes.c_wchar_p]
                dll.Everything3_ConnectW.restype = ctypes.c_void_p

                client = dll.Everything3_ConnectW("1.5a")
                if client and client != 0:
                    print("✅ Conexión exitosa")

                    # Probar si la DB está cargada
                    if "Everything3_IsDBLoaded" in available_functions:
                        dll.Everything3_IsDBLoaded.argtypes = [ctypes.c_void_p]
                        dll.Everything3_IsDBLoaded.restype = ctypes.c_bool

                        db_loaded = dll.Everything3_IsDBLoaded(client)
                        print(f"📋 DB cargada: {db_loaded}")

                    # Desconectar si está disponible
                    if "Everything3_Disconnect" in available_functions:
                        dll.Everything3_Disconnect.argtypes = [ctypes.c_void_p]
                        dll.Everything3_Disconnect.restype = None
                        dll.Everything3_Disconnect(client)
                        print("✅ Desconectado")

                else:
                    print("❌ Falló la conexión")

            except Exception as e:
                print(f"❌ Error en conexión básica: {e}")

    except Exception as e:
        print(f"❌ Error cargando DLL: {e}")


if __name__ == "__main__":
    test_everything3_functions()
