<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class SendMultiMails extends Mailable
{
    use Queueable, SerializesModels;

    public $senderName, $sendermail, $recievermail, $content, $subject, $file;

    public function __construct($senderName, $sendermail, $recievermail, $content, $subject, $file)
    {
        $this->senderName = $senderName;
        $this->sendermail = $sendermail;
        $this->recievermail = $recievermail;
        $this->content = $content;
        $this->subject = $subject;
        $this->file = $file;
    }

    public function build()
    {
        $email = $this->from($this->sendermail, $this->senderName)
                      ->to($this->recievermail)
                      ->subject($this->subject)
                      ->view('emails.multimail')
                      ->with(['content' => $this->content]);

        // Attach file if it exists
        if (!empty($this->file)) {
            $filePath = storage_path('app/public/' . $this->file);
            if (file_exists($filePath)) {
                $email->attach($filePath);
            } else {
                Log::error("Attachment file not found: " . $filePath);
            }
        }

        return $email;
    }
}