<?php
declare(strict_types=1);
header("Content-Type: application/json; charset=utf-8");

// 1) Paste YOUR Zapier Catch Hook URL here:
$hook = "https://hooks.zapier.com/hooks/catch/XXXXXX/YYYYYY/";

// 2) Build sample payload (use a real image/video URL if you want a true media test)
$payload = [
  "request_id"   => "test_" . uniqid(),
  "event"        => "published",
  "region"       => "US",
  "content_type" => "image", // image | video | text
  "title"        => "Zapier Test Post",
  "url"          => "https://example.com/test-post",
  "caption"      => "Zapier test caption ✅ #automation #socialmedia #yoga",
  "hashtags"     => ["#automation","#socialmedia","#yoga"],
  "image_url"    => "https://picsum.photos/seed/zaptest/1080/1080",
  "video_url"    => "" // put a real mp4 URL if testing video
];

$json = json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

if (!filter_var($hook, FILTER_VALIDATE_URL)) {
  http_response_code(400);
  echo json_encode(["ok"=>false,"error"=>"Invalid webhook URL"]);
  exit;
}

$ch = curl_init($hook);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  "Content-Type: application/json",
  "Content-Length: " . strlen($json)
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

$out  = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$err  = curl_error($ch);
curl_close($ch);

echo json_encode([
  "ok" => ($out !== false && $code < 400),
  "http_code" => $code,
  "curl_error" => $err,
  "zapier_response" => substr((string)$out, 0, 500),
  "sent" => $payload
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
