import requests
import os
from pathlib import Path

# Ensure a .env file exists next to this script and load variables from it.
def ensure_env_file(env_path: str, defaults: dict):
    """Create an env file at env_path with defaults if it doesn't exist."""
    p = Path(env_path)
    if not p.exists():
        try:
            p.parent.mkdir(parents=True, exist_ok=True)
            with p.open('w', encoding='utf-8') as f:
                for k, v in defaults.items():
                    f.write(f"{k}={v}\n")
            print(f"Created env file: {env_path}")
        except Exception as e:
            print(f"Failed to create env file {env_path}: {e}")

def load_env_file(env_path: str):
    """Load KEY=VALUE lines from env_path into os.environ if not already set.

    Supports simple lines, ignores blank lines and lines starting with '#'.
    """
    p = Path(env_path)
    if not p.exists():
        return
    try:
        with p.open('r', encoding='utf-8') as f:
            for raw in f:
                line = raw.strip()
                if not line or line.startswith('#'):
                    continue
                if '=' not in line:
                    continue
                k, v = line.split('=', 1)
                k = k.strip()
                v = v.strip().strip('"').strip("'")
                if k and k not in os.environ:
                    os.environ[k] = v
    except Exception as e:
        print(f"Failed to read env file {env_path}: {e}")

# Path to .env in the same folder as this script
_env_path = os.path.join(os.path.dirname(__file__), '.env')
_defaults = {
    'SCHLOTER_API_URL': 'http://schloter.api.ping-mee.de/quotes/random',
    'TEAMS_WEBHOOK_URL': 'https://outlook.office.com/webhook/REPLACE_WITH_YOUR_WEBHOOK',
    'SCHLOTER_API_TOKEN': '?2%?fK+@%Y9wy!f6'
}

ensure_env_file(_env_path, _defaults)
load_env_file(_env_path)

# Where to fetch a quote from (API)
API_URL = os.environ.get('SCHLOTER_API_URL', _defaults['SCHLOTER_API_URL'])

# Microsoft Teams Incoming Webhook URL (Workflows / connectors)
TEAMS_WEBHOOK_URL = os.environ.get('TEAMS_WEBHOOK_URL', _defaults['TEAMS_WEBHOOK_URL'])

# Static token for the API (can be set via environment variable)
API_TOKEN = os.environ.get('SCHLOTER_API_TOKEN', _defaults['SCHLOTER_API_TOKEN'])

def get_quote():
    """Fetch a quote from the API using a Bearer token.

    Returns the quote string or raises on HTTP error.
    """
    headers = {"Authorization": f"Bearer {API_TOKEN}"}
    resp = requests.get(API_URL, headers=headers, timeout=10)
    resp.raise_for_status()
    data = resp.json()
    return data.get("quote", "No quote found.")

def post_to_teams(quote):
    """Post the quote to a Teams Workflows (incoming webhook) URL.

    Teams incoming webhooks accept a simple JSON payload like {"text": "..."}.
    This function posts that payload and raises on failure.
    """
    payload = {"text": quote}
    headers = {"Content-Type": "application/json"}
    try:
        resp = requests.post(TEAMS_WEBHOOK_URL, json=payload, headers=headers, timeout=10)
        resp.raise_for_status()
    except requests.RequestException as e:
        print(f"Failed to post to Teams webhook: {e}")
        raise
    else:
        print("Posted to Teams webhook successfully.")


if __name__ == "__main__":
    # Fetch a quote and post it to Teams. Use env vars to override defaults.
    quote = get_quote()
    print("Quote fetched:", quote)
    # Uncomment the next line to actually send to Teams (ensure TEAMS_WEBHOOK_URL is set)
    # post_to_teams(quote)
# ...existing code from schloters_dailys.py...
