<?php
/**
 * ZeptoMail Mailer Implementation.
 *
 * @package NimblyMail\SMTP
 *
 * @since 1.0.0
 */

namespace NimblyMail\SMTP\Mailers;

use PHPMailer\PHPMailer\PHPMailer;

defined( 'ABSPATH' ) || exit;

/**
 * Class ZeptoMailMailer
 *
 * Sends email through Zoho ZeptoMail's transactional email API. The
 * mailer injects a custom SMTP transport subclass that intercepts
 * `data()` and posts the payload to the regional ZeptoMail endpoint
 * (`api.zeptomail.{region}/v1.1/email`) instead of opening a real SMTP
 * connection.
 *
 * @since 1.0.0
 */
class ZeptoMailMailer extends AbstractMailer {

    /**
     * Get the mailer slug.
     *
     * @since 1.0.0
     *
     * @return string The slug used as the `mail.mailer` option value.
     */
    public function get_slug(): string {
        return 'zeptomail';
    }

    /**
     * Get the mailer's human-readable title.
     *
     * @since 1.0.0
     *
     * @return string Translated provider title.
     */
    public function get_title(): string {
        return __( 'ZeptoMail', 'nimblymail-smtp' );
    }

    /**
     * Get a short description for the mailer card UI.
     *
     * @since 1.0.0
     *
     * @return string Translated short description.
     */
    public function get_description(): string {
        return __( 'Transactional email service by Zoho.', 'nimblymail-smtp' );
    }

    /**
     * Get the settings schema for this mailer as a section descriptor.
     *
     * @since 1.0.0
     *
     * @return array Section descriptor consumed by SettingsProvider.
     */
    public function get_settings_fields(): array {
        return array(
            'id'          => 'zeptomail_settings',
            'title'       => __( 'ZeptoMail', 'nimblymail-smtp' ),
            'description' => __( 'Connect to the ZeptoMail API.', 'nimblymail-smtp' ),
            'help_url'    => 'https://www.zoho.com/zeptomail/help/api-keys.html',
            'fields'      => array(
                array(
                    'id'          => 'api_key',
                    'group'       => 'zeptomail',
                    'title'       => __( 'Send Mail Token', 'nimblymail-smtp' ),
                    'type'        => 'password',
                    'required'    => true,
                    'encrypted'   => true,
                    'placeholder' => 'Zoho-enczapikey xxxxxxxx',
                    'description' => __( 'Your ZeptoMail Send Mail Token.', 'nimblymail-smtp' ),
                ),
                array(
                    'id'       => 'region',
                    'group'    => 'zeptomail',
                    'title'    => __( 'Region', 'nimblymail-smtp' ),
                    'type'     => 'select',
                    'required' => true,
                    'default'  => 'com',
                    'options'  => array(
                        'com'    => __( 'United States (com)', 'nimblymail-smtp' ),
                        'eu'     => __( 'Europe (eu)', 'nimblymail-smtp' ),
                        'in'     => __( 'India (in)', 'nimblymail-smtp' ),
                        'com.cn' => __( 'China (com.cn)', 'nimblymail-smtp' ),
                        'com.au' => __( 'Australia (com.au)', 'nimblymail-smtp' ),
                    ),
                ),
            ),
        );
    }

    /**
     * Configure the PHPMailer instance to send through ZeptoMail.
     *
     * Forces SMTP mode so we can swap the transport, then injects a
     * ZeptoMailTransport subclass that intercepts `data()` and posts the
     * payload to the regional ZeptoMail REST endpoint. The region falls
     * back to the registered default (`com`) when the option is unset.
     *
     * @since 1.0.0
     *
     * @param PHPMailer $phpmailer The PHPMailer instance about to send.
     *
     * @return void
     */
    public function configure( PHPMailer $phpmailer ): void {
        $phpmailer->isSMTP();
        $phpmailer->SMTPAutoTLS = false;

        $api_key = (string) $this->get_option( 'api_key' );
        $region  = (string) $this->get_option( 'region' );

        if ( '' === $region ) {
            $region = 'com';
        }

        $phpmailer->setSMTPInstance( new ZeptoMailTransport( $phpmailer, $api_key, $region ) );
    }

    /**
     * Get this mailer's deliverability audit requirements.
     *
     * ZeptoMail's SPF include domain matches the Zoho region the account is
     * provisioned in (com, eu, in, au, etc.). The required region is read
     * from the saved option so the audit checks the right include.
     *
     * @since 1.0.0
     *
     * @return array{spf_includes: string[], dkim_selectors: string[], docs_url: string}
     */
    public function get_audit_requirements(): array {
        $region = (string) $this->get_option( 'region' );
        if ( '' === $region ) {
            $region = 'com';
        }

        return array(
            'spf_includes'   => array( 'zeptomail.zoho.' . $region ),
            'dkim_selectors' => array( 'zmail' ),
            'docs_url'       => 'https://www.zoho.com/zeptomail/help/spf-and-dkim-configuration.html',
        );
    }

    /**
     * Get the connectivity endpoints for the ZeptoMail API mailer.
     *
     * @since 1.0.0
     *
     * @return array<array<string, mixed>>
     */
    public function get_connectivity_endpoints(): array {
        $region = (string) $this->get_option( 'region' );
        if ( '' === $region ) {
            $region = 'com';
        }

        return array(
            array(
                'type' => 'api',
                'url'  => "https://api.zeptomail.{$region}/v1.1/email",
            ),
        );
    }
}
