import requests
import json
from typing import List, Dict, Any

def send_to_n8n_webhook(
    webhook_url: str,
    role: str,
    start_statement: str,
    timeline: List[str],
    constraints: List[str],
    timeout: int = 10,
) -> Dict[str, Any]:
    """
    Sends action suggestion request to n8n webhook.

    Args:
      webhook_url: Full URL to your n8n webhook endpoint.
      role: Role requesting advice (e.g. 'engineer', 'pm').
      start_statement: Main decision statement string.
      timeline: List of up to 5 concise prior decision summaries.
      constraints: List of active constraints/policies strings.
      timeout: HTTP timeout in seconds.

    Returns:
      Parsed JSON response from n8n webhook.

    Raises:
      requests.RequestException for HTTP/network errors.
      json.JSONDecodeError if response is not valid JSON.
      ValueError if response fails schema validation.
    """
    payload = {
        "role": role,
        "start_statement": start_statement,
        "timeline": timeline[:5],  # cap to 5
        "constraints": constraints,
    }

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

    response = requests.post(webhook_url, json=payload, headers=headers, timeout=timeout)
    response.raise_for_status()

    data = response.json()
