<?php



require_once(__DIR__ . "/../vendor/autoload.php");

require_once(__DIR__ . "/../config/config.php");





// /api/acc/branch/0/import #queue

// /api/acc/branch/(id)/import

// /api/acc/branch/0/sync #queue

// /api/acc/branch/(id)/sync

// /api/acc/product/(id)/import

// /api/acc/product/(id)/pictures

// /api/acc/product/(id)/gpt



$url = $_GET["api"] ?? "";

$path = explode('/', $url);



if (count($path) != 5) {

	http_response_code(400);

	die(json_encode(["result" => false, "error" => "bad request"]));

}



$jobQueue = new n0nag0n\Job_Queue('mysql', [

	'mysql' => [

		'use_compression' => false,

	]

]);



$PDO = new PDO('mysql:dbname=' . INTDB_DATABASE . ';host=' . INTDB_HOST, INTDB_USER, INTDB_PASS);

$jobQueue->addQueueConnection($PDO);



$pipeline = "{$path[1]}_{$path[2]}";

$body = json_decode(file_get_contents("php://input"), 1) ?: [];



$jobQueue->selectPipeline($pipeline);

$job = $jobQueue->addJob(json_encode([ 'itemId' => $path[3], 'itemAction' => $path[4], 'body' => $body ]));



http_response_code(200);

die(json_encode(["result" => true, "text" => "request queued"]));

