<?php

namespace App\Classes\Notifications;


use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\PackingList;
use App\Models\PasswordReset;
use App\Models\User;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Storage;

class InvoiceIssuedEmail extends AbstractEmail
{

    /** @var User */
    private $user;

    /** @var PackingList */
    private $packingList;

    /**
     * @param User $user
     * @param PackingList $packingList
     */
    public function __construct(User $user, PackingList $packingList)
    {
        $this->user = $user;
        $this->packingList = $packingList;
    }


    public function toMail()
    {
        $invoice = $this->packingList->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->where('status', ApprovalStatus::APPROVED)->first();
        $invoiceDocument = $invoice->documents()->first()->files;


        return (new MailMessage)
            ->subject('Att: '.$this->user->name.' - Invoice for order no.'. $this->packingList->owner->reference)
            ->attach(Storage::disk('documents')->get($invoiceDocument->file->file_info->original->file), [
                'as' => 'name.pdf',
                'mime' => 'application/pdf',
            ])->view('emails.shipment.invoice', ['user' => $this->user, 'packingList' => $this->packingList]);
    }


}
