<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SendEmailFromSystem extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $details = "";
    public $subject;
    public $newEmail;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct( User $user, $details, $subject, $newEmail)
    {
        $this->user = $user;
        $this->details = $details;
        $this->subject = $subject;
        $this->newEmail = $newEmail;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $send = $this->from($this->user->smtp_email, $this->user->firstName." ".$this->user->lastName)
            ->replyTo($this->user->smtp_email, $this->user->firstName." ".$this->user->lastName)
            ->subject($this->subject)
            ->view('emails.send-email-template');
        /**We control what is being attached**/
        if(count($this->newEmail->attachments) > 0){
            foreach ($this->newEmail->attachments as $attachment) {
                $send = $send->attach(public_path()."/storage".preg_replace('/public/', "", $attachment->path));
            }
        }
        /**Return the Send Email**/
        return $send;
    }
}
