"""
{{ display_name }} - Validate node.

This module handles webhook event validation.
"""

import uuid
from datetime import datetime, timezone

from state import {{ class_name }}State

# Define supported event types
SUPPORTED_EVENTS = [
    "created",
    "updated",
    "deleted",
    "triggered",
]


async def validate(state: {{ class_name }}State) -> dict:
    """Validate the incoming webhook event.

    Args:
        state: Current handler state.

    Returns:
        Dictionary with state updates including validation status.
    """
    context = state["context"]
    input_data = state["input_data"]

    event_type = input_data.get("event_type", "")
    payload = input_data.get("payload", {})

    context.log_progress("Validating", f"Checking event: {event_type}")

    # Generate event ID
    event_id = str(uuid.uuid4())

    # Validate event
    validation_errors = []

    if not event_type:
        validation_errors.append("Missing event_type")
    elif event_type not in SUPPORTED_EVENTS:
        validation_errors.append(f"Unsupported event_type: {event_type}")

    if not payload:
        validation_errors.append("Empty payload")

    is_valid = len(validation_errors) == 0

    if is_valid:
        context.log_success("Validation passed", f"Event ID: {event_id}")
    else:
        context.log_warning("Validation failed", ", ".join(validation_errors))

    return {
        "current_step": "validated",
        "event_id": event_id,
        "event_type": event_type,
        "payload": payload,
        "headers": input_data.get("headers", {}),
        "is_valid": is_valid,
        "validation_errors": validation_errors,
        "metadata": {
            "validated_at": datetime.now(timezone.utc).isoformat(),
        },
    }
