#!/usr/bin/env python3
"""Publish due scheduled social posts."""
from __future__ import annotations

import json
import sys
import urllib.request

COCKPIT_URL = "http://127.0.0.1:8600"
TOOLS_URL = "http://127.0.0.1:8700"


def main() -> int:
    try:
        req = urllib.request.Request(
            f"{COCKPIT_URL}/api/admin/scheduled-posts/publish-due",
            data=b"{}",
            headers={"Content-Type": "application/json"},
            method="POST",
        )
        with urllib.request.urlopen(req, timeout=120) as resp:
            data = json.loads(resp.read().decode("utf-8"))
        print(json.dumps(data))
        return 0
    except urllib.error.HTTPError as exc:
        if exc.code == 404:
            print("publish-due endpoint not yet available — skipped")
            return 0
        print(f"publish failed: {exc}", file=sys.stderr)
        return 1
    except Exception as exc:
        print(f"publish failed: {exc}", file=sys.stderr)
        return 1


if __name__ == "__main__":
    raise SystemExit(main())
