<?php
/**
 * @author Ulrich Hecht <ulrich.hecht@hecht-software.de>
 * @date 02/21/2017
 */

namespace AppBundle\Export;


use AppBundle\Entity\ContactRequest;
use Monolog\Logger;

class ContactSternToursCrmExporter extends SternToursCrmExporter
{

    public function __construct(Logger $logger)
    {
        parent::__construct($logger);
    }

    public function process(ContactRequest $contactRequest)
    {
        $data = ['lead' => [
            'customerForm' => [
                'salutation_id' => $contactRequest->getSalutation(),
                'name' => $contactRequest->getLastName(),
                'firstname' => $contactRequest->getFirstName(),
                'street' => $contactRequest->getStreetAddress(),
                'zip' => $contactRequest->getZipCode(),
                'city' => $contactRequest->getCity(),
                'country_id' => $contactRequest->getNation(),
                'phone' => $contactRequest->getPhone(),
                'phonemobile' => $contactRequest->getMobilePhone(),
                'email' => $contactRequest->getEmail()
            ],
            'request_date' => (new \DateTime())->format('Y-m-d'),
            'sf_guard_user_id' => self::API_USER_ID,
            'status_id' => 10, // 'Angebot erstellen'
            'travelperiod_start' => $contactRequest->getStart(),
            'travelperiod_end' => $contactRequest->getEnd(),
            //'travelcategory_id'
            'is_closed' => 0,
            'website_id' => self::WEBSITE_ID,
            'initialcontacttype_id' => 1,
            'travelperiod_length' => $contactRequest->getDuration(),
            'remarks' => $contactRequest->getNotes(),
            'pax' => $contactRequest->getTravelerCount(),
        ]];

        $resp = $this->httpPost('lead', $data);

        if (!$resp['success'])
        {
            $this->logger->error(get_class($this). ': Failed submitting contact request to CRM');
            $this->logger->error('*** Submitted data: '. json_encode($data));
            $this->logger->error('*** Server response: '. $resp['content']);
        }

        return $resp['location'] ?? null;
    }
}