<?php

declare(strict_types=1);

namespace App\Listeners;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Arr;
use App\Models\EmailLog;

class LogSentMessage
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \Illuminate\Mail\Events\MessageSent  $message
     * @return void
     */
    public function handle(MessageSent $event)
    {
        $userID = Arr::get($event->data, 'userID');
        $from = current($event->message->getFrom());
        $to = current($event->message->getTo());
        $sent = $event->message;

        EmailLog::create([
            'user_id' => $userID,
            'to_address' => $to->getAddress(), // 수신
            'to_name' => $to->getName(), // 수신
            'from_address' => $from->getAddress(), // 발신
            'from_name' => $from->getName(), // 발신
            'subject' => $sent->getSubject(), // 제목
            'content' => $sent->getHtmlBody(), // 내용
            'file_name' => null,
            'file_size' => null
        ]);
    }
}
