import google.generativeai as genai
import time

# 🔴 COLE AQUI A SUA CHAVE NOVA (A que colocou no servidor_app.py)
MINHA_CHAVE = "AIzaSyDplOOzkJyi_r6vAlMH5ZbayxSW9S9dSdk"

print("--- INICIANDO DIAGNÓSTICO DA IA ---")
print(f"1. A verificar chave: {MINHA_CHAVE[:10]}...")

try:
    genai.configure(api_key=MINHA_CHAVE)
    print("2. Configuração: OK")
except Exception as e:
    print(f"❌ ERRO NA CONFIGURAÇÃO: {e}")
    exit()

print("3. A tentar enviar uma pergunta de teste à Google...")

model = genai.GenerativeModel('gemini-1.5-flash')
prompt = "Responda apenas com a palavra 'FUNCIONA' se estiver a receber esta mensagem."

try:
    # Medir o tempo de resposta
    inicio = time.time()
    response = model.generate_content(prompt)
    fim = time.time()
    
    print(f"4. Resposta Recebida: '{response.text.strip()}'")
    print(f"5. Tempo demorado: {round(fim - inicio, 2)} segundos")
    print("\n✅ CONCLUSÃO: A SUA CHAVE E A CONEXÃO ESTÃO PERFEITAS!")
    
except Exception as e:
    print(f"\n❌ ERRO FATAL: A IA NÃO RESPONDEU.")
    print(f"Detalhe do erro: {e}")
    print("\nCausas prováveis:")
    print("- A chave está errada ou foi bloqueada.")
    print("- O servidor não tem internet.")
