<?php namespace App\Jobs\Nav;

use App\Models\Order;
use App\Services\Navision\OrdersService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;

class CreateOrder implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * The webhook data
     *
     * @var Order
     */
    public $order;

    /**
     * Create a new job instance.
     *
     * @param Order $data - The order to be integrated with NAV.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(OrdersService $service)
    {
        Log::info('Creating order in NAV: ' . $this->order->number . ' (ID: ' . $this->order->id . ')');
        $order = $service->createNavOrder($this->order);
    }

    /**
     * Handle a job failure.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function failed(Throwable $exception)
    {
        //Mail::to(config('mail.alerts_to'))->send(new AlertNotification('Job Failed: OrdersPaidJob<br><br>' . nl2br($exception->getTraceAsString())));
    }
}
