<?php

namespace App\Classes\Modules\Orders\Processors;


use App\Classes\Excel\Import\OrdersExport;
use App\Classes\Modules\ControllerLogic\Exceptions\InternalServerErrorException;
use App\Classes\Modules\Orders\DataTransferObjects\StepObject;
use App\Classes\ValueObjects\Constants\OrderSteps;
use App\Models\Order;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Mail;
use Maatwebsite\Excel\Facades\Excel;

class WarehouseReceivesOrder extends AbstractStepProcessor
{

    /** @var string  */
    const STEP_REFERENCE = OrderSteps::ORDER_WAREHOUSE_RECEIVING_STEP;

    /** @var int */
    private $orderId;

    /** @var StepObject */
    private $step;

    /**
     * WarehousePackOrder constructor.
     * @param int $orderId
     * @param string $completeDate
     */
    public function __construct(int $orderId, string $completeDate = null)
    {
        $this->orderId = $orderId;
        $this->step = new StepObject(self::STEP_REFERENCE, $completeDate);

    }

    /**
     * @return bool
     */
    public function validator(): bool {
        return true;
    }


    /**
     * @return JsonResponse
     * @throws InternalServerErrorException
     */
    public function execute(): JsonResponse {
        Mail::send('emails.received_orders_email', [], function ($message) {
            $message->from('noreply@izyin.com', 'IZYIM');
            $message->to('uldvstar@gmail.com', 'Omair Saleh')->subject('Received orders report '.now()->format('dd/mm/YY'));
            $message->attach(
                Excel::download(Order::find($this->orderId),
                    'report_'.now()->format('dd_mm_YY').'.xlsx')
                ->getFile(), ['as' => 'report_'.now()->format('dd/mm/YY').'.xlsx']);

        });

        return $this->handler($this->orderId, $this->step);
    }

}