#!/usr/bin/env python3
# Ligbox Ops webhook integration for Wazuh manager
# Copyright (C) 2026 Ibytera / Ligbox — based on Wazuh slack.py

import json
import os
import sys

try:
    import requests
except ImportError:
    print("pip install requests required")
    sys.exit(1)

ALERT_INDEX = 1
WEBHOOK_INDEX = 3
LOG_FILE = "/var/ossec/logs/integrations.log"
SECRET = os.environ.get("LIGBOX_OPS_WEBHOOK_SECRET", "ligbox-wazuh-dev-secret")


def debug(msg: str) -> None:
    with open(LOG_FILE, "a") as f:
        f.write(msg + "\n")


def main(args):
    if len(args) < 4:
        sys.exit(2)
    alert_file = args[ALERT_INDEX]
    hook_url = args[WEBHOOK_INDEX]
    with open(alert_file, "r") as f:
        alert = json.load(f)
    headers = {
        "Content-Type": "application/json",
        "X-Webhook-Secret": SECRET,
    }
    try:
        res = requests.post(hook_url, json=alert, headers=headers, timeout=10)
        debug(f"ligbox-ops POST {hook_url} -> {res.status_code} {res.text[:200]}")
    except Exception as exc:
        debug(f"ligbox-ops ERROR: {exc}")
        sys.exit(1)


if __name__ == "__main__":
    main(sys.argv)
