<?php

namespace App\Services\BroadcastEmail;

class BroadcastEmailSenderOptionsService
{
    public function listOptions(): array
    {
        $json = getenv('MAIL_SENDERS') ?: env('MAIL_SENDERS', '{}');
        $arr = json_decode($json, true);

        if (!is_array($arr) || empty($arr)) {
            $smtpUser = getenv('SMTP_USER') ?: '';
            $name = 'Al Rahma Sunday School';
            return [[
                'key' => 'general',
                'label' => $name . ($smtpUser ? " <{$smtpUser}>" : ''),
            ]];
        }

        $out = [];
        foreach ($arr as $key => $info) {
            $name = $info['name'] ?? 'Sender';
            $email = $info['email'] ?? '';
            $out[] = [
                'key' => (string) $key,
                'label' => trim($name . ($email ? " <{$email}>" : '')),
            ];
        }

        return $out;
    }
}
