<?php

namespace App\Classes\Modules\Bookings\ControllersLogic;

use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Jobs\Commands\V2\ProcessSalesInvoiceReportV2CommandJob;
use App\Classes\Modules\Bookings\DataTransferObjects\UpdateBookingSalesInvoiceDTO;
use App\Classes\Modules\Bookings\Standards\Rules\CanUpdateBookingSalesInvoice;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UpdateBookingSalesInvoiceLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Updated Booking with Sales Invoice',
            'message' => 'You have successfully updated Booking with Sales Invoice'
        ];
    }

    /** @var CanUpdateBookingSalesInvoice */
    private $canUpdateBookingSalesInvoice;


    /**
     * UpdateBookingSalesInvoiceLogic constructor.
     * @param CanUpdateBookingSalesInvoice $canUpdateBookingSalesInvoice
     */
    public function __construct(
        CanUpdateBookingSalesInvoice $canUpdateBookingSalesInvoice
    )
    {
        $this->canUpdateBookingSalesInvoice = $canUpdateBookingSalesInvoice;
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $data = [
            'docno' => $request->input('docno', $request->input('DocNo')),
            'docdate' => $request->input('docdate', $request->input('DocDate')),
            'debtorcode' => $request->input('debtorcode', $request->input('DebtorCode')),
            'ref' => $request->input('ref', $request->input('Ref')),
            'shipinfo' => $request->input('shipinfo', $request->input('ShipInfo')),
            'accno' => $request->input('accno', $request->input('AccNo')),
            'detaildescription' => $request->input('detaildescription', $request->input('DetailDescription')),
            'furtherdescription' => $request->input('furtherdescription', $request->input('FurtherDescription')),
            'classification' => $request->input('classification', $request->input('Classification')),
            'deptno' => $request->input('deptno', $request->input('DeptNo')),
            'qty' => $request->input('qty', $request->input('Qty')),
            'unitprice' => $request->input('unitprice', $request->input('UnitPrice')),
            'submiteinvoice' => $request->input('submiteinvoice', $request->input('SubmitEinvoice')),
            'consolidatedeinvoice' => $request->input('consolidatedeinvoice', $request->input('ConsolidatedEinvoice')),
            'einvoicevalidationlink' => $request->input('einvoicevalidationlink', $request->input('EInvoiceValidationLink')),
        ];

        $dto = new UpdateBookingSalesInvoiceDTO($data);
        $this->canUpdateBookingSalesInvoice->passes($dto);

        ProcessSalesInvoiceReportV2CommandJob::dispatch($dto->toArray());

        return $this->response([]);
    }
}
