<?php

namespace App\Notify\Channels;

use App\Notify\Contracts\Channel;
use App\Notify\Message;
use Illuminate\Support\Facades\Mail;

/**
 * Email via Laravel's Mail facade. SMTP/SES/Mailgun whatever's configured
 * in .env (MAIL_* vars). For local dev, leave MAIL_MAILER=log to capture
 * sent emails in storage/logs/laravel.log without a real SMTP server.
 */
class EmailChannel implements Channel
{
    public function key(): string { return 'email'; }

    public function requiredConfig(): array { return ['to']; }

    public function send(array $config, Message $message): array
    {
        $to    = $config['to'];
        $cc    = $config['cc'] ?? null;
        $color = $message->color();

        $html = view('emails.notify', [
            'message' => $message,
            'color'   => $color,
        ])->render();

        try {
            Mail::html($html, function ($mail) use ($to, $cc, $message) {
                $mail->to($to);
                if ($cc) $mail->cc($cc);
                $mail->subject('[' . strtoupper($message->severity) . '] ' . $message->title);
            });
            return ['ok' => true, 'response' => null];
        } catch (\Throwable $e) {
            return ['ok' => false, 'error' => $e->getMessage()];
        }
    }
}
