<?php

use Dealerdirect\Agreements\Factory;

/**
 * @function getStorage
 * @function getEmailClient
 * @function getConnection
 *
 * @var int  $reference
 * @var int  $deliverId
 * @var bool $showFile
 */
require_once __DIR__ . '/bootstrap.php';

$encryptionKey = $_GET['binary_key'] ?? 'EmsbBUFwIbL2Sq2vBj519AyE8Gazb2Z6nUOPHPQPnX4';
$domains = [
    'carNl' => 'https://ikwilvanmijnautoaf.nl.aio.test',
    'carBe' => 'https://ikwilvanmijnautoaf.be.aio.test',
    'carDe' => 'https://ichwillmeinautoloswerden.de.aio.test',
    'motorNl' => 'https://ikwilvanmijnmotoraf.nl.aio.test',
    'motorBe' => 'https://ikwilvanmijnmotoraf.be.aio.test',
    'motorDe' => 'https://ichwillmeinmotorradloswerden.de.aio.test',
    'scooterNl' => 'https://ikwilvanmijnscooteraf.nl.aio.test',
    'bicycleNl' => 'https://ikwilvanmijnfietsaf.nl.aio.test',
    'marktplaatsNl' => 'http://localhost:8000/#',
];

$salesAgreementFactory = Factory::forSalesAgreement(
    getStorage('/tmp/verkoopovereenkomsten'),
    Factory::getModelByReference($reference, getConnection(), $deliverId)
);

$salesAgreementFactory->delete();
$salesAgreementFactory->create();
if (!$showFile) {
    $salesAgreementFactory->send(getEmailClient(), $domains, $encryptionKey);

    echo 'Sales Agreement is created and send';
    exit;
}

header('Content-Type: application/pdf');
echo $salesAgreementFactory->show();
