import pathlib

content = '''\
import os
import sys
sys.path.insert(0, ".")

from dotenv import load_dotenv
load_dotenv()

from mistralai import Mistral


def test_mistral_api():
    api_key = os.environ.get("MISTRAL_API_KEY")

    if not api_key:
        print("No MISTRAL_API_KEY found in environment")
        return False

    if api_key == "your-mistral-api-key-here":
        print("MISTRAL_API_KEY is still the placeholder value")
        return False

    print(f"API Key found: {api_key[:6]}...{api_key[-4:]}")

    try:
        client = Mistral(api_key=api_key)
        print("Mistral client created successfully")

        models_to_try = [
            "mistral-small-latest",
            "mistral-medium-latest",
            "mistral-large-latest",
        ]

        for model_name in models_to_try:
            try:
                print(f"Testing model: {model_name}")
                resp = client.chat.complete(
                    model=model_name,
                    messages=[{"role": "user", "content": "Hello, world!"}],
                )
                text = resp.choices[0].message.content
                print(f"{model_name} working! Response: {text[:60]}...")
                return True
            except Exception as e:
                print(f"{model_name} failed: {e}")
                continue

        print("All Mistral models failed")
        return False

    except Exception as e:
        print(f"Mistral API error: {e}")
        return False


if __name__ == "__main__":
    print("Testing Mistral AI API Connection...")
    print("=" * 50)

    success = test_mistral_api()

    if success:
        print("\\nMistral AI is working correctly!")
    else:
        print("\\nMistral API test failed.")
        print("\\nPossible solutions:")
        print("1. Check if the MISTRAL_API_KEY in .env is valid")
        print("2. Verify internet connection")
        print("3. Check https://status.mistral.ai/ for service status")
        print("4. Generate a new API key from https://console.mistral.ai/api-keys")
'''

pathlib.Path("test_gemini.py").write_text(content, encoding="utf-8")
print("Written successfully")
