<?php
namespace EasyChat\Library\Email;





final class EmailProviderCatalog {
    public const MANUAL = 'manual';

    public static function all(): array {
        return [
            'yandex' => [
                'id' => 'yandex', 'name' => 'Яндекс Почта', 'mark' => 'Я',
                'domains' => ['yandex.ru', 'ya.ru', 'yandex.com', 'yandex.kz', 'yandex.by'],
                'imap_host' => 'imap.yandex.ru', 'imap_port' => 993,
                'smtp_host' => 'smtp.yandex.ru', 'smtp_port' => 465,
                'help_url' => 'https://yandex.ru/support/yandex-360/customers/mail/ru/mail-clients/others',
            ],
            'mailru' => [
                'id' => 'mailru', 'name' => 'Почта Mail',
                'domains' => ['mail.ru', 'inbox.ru', 'list.ru', 'bk.ru', 'internet.ru'],
                'imap_host' => 'imap.mail.ru', 'imap_port' => 993,
                'smtp_host' => 'smtp.mail.ru', 'smtp_port' => 465,
                'help_url' => 'https://help.mail.ru/mail/login/mailer/',
            ],
            'vk_workspace' => [
                'id' => 'vk_workspace', 'name' => 'VK WorkSpace',
                'domains' => [],
                'imap_host' => 'imap.mail.ru', 'imap_port' => 993,
                'smtp_host' => 'smtp.mail.ru', 'smtp_port' => 465,
                'help_url' => 'https://workspace.vk.ru/docs/saas/user-guides/ru/mail/clients',
            ],
            'rambler' => [
                'id' => 'rambler', 'name' => 'Рамблер/почта',
                'domains' => ['rambler.ru', 'lenta.ru', 'autorambler.ru', 'myrambler.ru', 'ro.ru'],
                'imap_host' => 'imap.rambler.ru', 'imap_port' => 993,
                'smtp_host' => 'smtp.rambler.ru', 'smtp_port' => 465,
                'help_url' => 'https://help.rambler.ru/mail/mail-pochtovye-klienty/1275',
            ],
            'gmail' => [
                'id' => 'gmail', 'name' => 'Gmail',
                'domains' => ['gmail.com', 'googlemail.com'],
                'imap_host' => 'imap.gmail.com', 'imap_port' => 993,
                'smtp_host' => 'smtp.gmail.com', 'smtp_port' => 465,
                'help_url' => 'https://support.google.com/mail/answer/7126229',
            ],
            'icloud' => [
                'id' => 'icloud', 'name' => 'iCloud Mail',
                'domains' => ['icloud.com', 'me.com', 'mac.com'],
                'imap_host' => 'imap.mail.me.com', 'imap_port' => 993,
                'smtp_host' => 'smtp.mail.me.com', 'smtp_port' => 587,
                'help_url' => 'https://support.apple.com/102525',
            ],
            'yahoo' => [
                'id' => 'yahoo', 'name' => 'Yahoo Mail',
                'domains' => ['yahoo.com', 'yahoo.co.uk', 'yahoo.de', 'yahoo.fr', 'ymail.com', 'rocketmail.com'],
                'imap_host' => 'imap.mail.yahoo.com', 'imap_port' => 993,
                'smtp_host' => 'smtp.mail.yahoo.com', 'smtp_port' => 465,
                'help_url' => 'https://help.yahoo.com/kb/imap-internet-message-access-protocol-sln4075.html',
            ],
            'zoho' => [
                'id' => 'zoho', 'name' => 'Zoho Mail',
                'domains' => ['zoho.com', 'zohomail.com'],
                'imap_host' => 'imap.zoho.com', 'imap_port' => 993,
                'smtp_host' => 'smtp.zoho.com', 'smtp_port' => 465,
                'help_url' => 'https://www.zoho.com/mail/help/imap-access.html',
            ],
            'fastmail' => [
                'id' => 'fastmail', 'name' => 'Fastmail',
                'domains' => ['fastmail.com', 'fastmail.fm', 'fastmail.co.uk', 'fastmail.com.au', 'fastmail.de', 'fastmail.fr', 'fastmail.jp', 'fastmail.net', 'fastmail.org', 'fastmail.uk', 'fastmail.us'],
                'imap_host' => 'imap.fastmail.com', 'imap_port' => 993,
                'smtp_host' => 'smtp.fastmail.com', 'smtp_port' => 465,
                'help_url' => 'https://www.fastmail.help/hc/en-us/articles/1500000279921',
            ],
        ];
    }

    public static function find(string $id): ?array {
        $all = self::all();
        return $all[$id] ?? null;
    }

    public static function detect(string $email): ?array {
        $at = strrpos(strtolower(trim($email)), '@');
        if ($at === false) return null;
        $domain = substr(strtolower(trim($email)), $at + 1);
        foreach (self::all() as $provider) {
            if (in_array($domain, $provider['domains'], true)) return $provider;
        }
        return null;
    }

    public static function publicList(): array {
        return array_values(self::all());
    }
}
