<?php

namespace App\Domains\MessageSystem\Actions\SendMessage;

use App\Enumerations\MessageType;
use Illuminate\Support\Facades\Mail;

class SendMessageCommand {
    private SendMessageRequest $request;

    public function __construct(SendMessageRequest $request) {
        $this->request = $request;
    }

    public function send() : SendMessageResponse {
        $response =  new SendMessageResponse();

        foreach ($this->request->messageTypes as $messageType) {
            switch (true) {
                case $messageType->value === MessageType::EMAIL->value:
                    $this->sendAsEmail();
                    break;

                case $messageType->value === MessageType::INTERNAL->value:
                    $this->sendAsInternalMessage();
                    break;
            }
        }

        return $response;
    }

    private function sendAsEmail() {
        foreach ($this->request->recipient->getEmailAddresses() as $emailAddress) {
            Mail::html($this->request->message, function ($message) use ($emailAddress) {
                $message
                    ->to($emailAddress->getValue(), $this->request->recipient->getName())
                    ->subject($this->request->subject);
            });
        }
    }

    private function sendAsInternalMessage() {
    }
}
