<?php
namespace Eadesigndev\GLS\Helper;

use Eadesigndev\GLS\Model\GenerateAwb;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\HTTP\Client\CurlFactory;
use Magento\Framework\Serialize\Serializer\Json;

/**
 * GLS Helper - uses REST API for all operations
 */
#[\AllowDynamicProperties]
class Data extends AbstractHelper
{
    const API_URL_GET_PRINTED_TEST = "https://api.test.mygls.ro/ParcelService.svc/json/GetPrintedLabels";
    const API_URL_GET_PRINTED_PRODUCTION = "https://api.mygls.ro/ParcelService.svc/json/GetPrintedLabels";

    private ScopeConfigInterface $_scopeConfig;
    private CurlFactory $curlFactory;
    private Json $json;

    public function __construct(
        ScopeConfigInterface $scopeConfig,
        CurlFactory $curlFactory,
        Json $json
    )
    {
        $this->_scopeConfig = $scopeConfig;
        $this->curlFactory = $curlFactory;
        $this->json = $json;
    }

    /**
     * Get AWB document (PDF label) using GetPrintedLabels API
     * Uses PrepareLabels + GetPrintedLabels workflow
     *
     * @param string|int $parcelId Parcel ID (saved as awb_number when AWB was generated)
     * @param string|int $pickupId Pickup ID (optional, not used for GLS)
     * @param string $printerType Printer type: 'Connect' or 'A4_4x1' (default)
     * @param int $printPosition Print position for A4 (1-4), default 1
     * @return string PDF content as binary string
     * @throws \Exception
     */
    public function getAwbDocument($parcelId, $pickupId = null, $printerType = 'A4_4x1', $printPosition = 1)
    {
        // Get GenerateAwb instance
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $generateAwb = $objectManager->get(GenerateAwb::class);

        // Call GetPrintedLabels API via GenerateAwb
        return $generateAwb->getPrintedLabel((string)$parcelId, $printerType, $printPosition);
    }
}
