<?php
namespace Domain\Integrations;

class ApiloLogger
{
    /**
     * @param \medoo $db
     * @param string $action  np. 'send_order', 'payment_sync', 'status_sync', 'status_poll'
     * @param int|null $orderId
     * @param string $message
     * @param mixed $context  dane do zapisania jako JSON (request/response)
     */
    public static function log($db, string $action, ?int $orderId, string $message, $context = null): void
    {
        $contextJson = null;
        if ($context !== null) {
            $contextJson = is_string($context)
                ? $context
                : json_encode($context, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        }

        $db->insert('pp_log', [
            'action' => $action,
            'order_id' => $orderId,
            'message' => $message,
            'context' => $contextJson,
            'date' => date('Y-m-d H:i:s'),
        ]);
    }
}
