import requests
import json
import os

def send_teams_message(webhook_url, message):
    """
    Sends a simple message to a Microsoft Teams channel using a webhook.

    Args:
        webhook_url (str): The URL of the incoming webhook.
        message (str): The plain text message to send.

    Returns:
        bool: True if the message was sent successfully (HTTP 200), False otherwise.
    """
    if not webhook_url:
        print("Error: TEAMS_WEBHOOK_URL is not set.")
        return False

    headers = {
        "Content-Type": "application/json"
    }

    payload = {
        "text": message
    }

    try:
        response = requests.post(webhook_url, headers=headers, data=json.dumps(payload), timeout=10)
        
        if response.status_code == 200:
            print("Message sent successfully to Teams.")
            return True
        else:
            print(f"Failed to send message. Status code: {response.status_code}")
            print(f"Response: {response.text}")
            return False
            
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while sending the request: {e}")
        return False

if __name__ == "__main__":
    # The webhook URL is taken directly from the project description for this test.
    # In a real application, this should be loaded from an environment variable.
    webhook_url = "https://wacklergroup.webhook.office.com/webhookb2/fe728cde-790c-4190-b1d3-be393ca0f9bd@6d85a9ef-3878-420b-8f43-38d6cb12b665/IncomingWebhook/e9a8ee6157594a6cab96048cf2ea2232/d26033cd-a81f-41a6-8cd2-b4a3ba0b5a01/V2WFmjcbkMzSU4f6lDSdUOM9VNm7F7n1Th4YDiu3fLZ_Y1"
    
    test_message = "🤖 This is a test message from the Gemini Trading Twins Engine. If you see this, the webhook is working. [31988f42]"

    send_teams_message(webhook_url, test_message)
