<?php
/**
 * Resend Mailer Implementation.
 *
 * @package NimblyMail\SMTP
 */

namespace NimblyMail\SMTP\Mailers;

use PHPMailer\PHPMailer\PHPMailer;

defined( 'ABSPATH' ) || exit;

/**
 * Class ResendMailer
 *
 * Implements sending via the Resend API.
 *
 * @since 1.0.0
 */
class ResendMailer extends AbstractMailer {

    /**
     * Get the mailer slug.
     *
     * @return string
     */
    public function get_slug(): string {
        return 'resend';
    }

    /**
     * Get the mailer readable title.
     *
     * @return string
     */
    public function get_title(): string {
        return __( 'Resend', 'nimblymail-smtp' );
    }

    /**
     * Get the mailer description shown in the mailer-cards selector.
     *
     * @since 1.0.0
     *
     * @return string
     */
    public function get_description(): string {
        return __( 'Modern email API for developers.', 'nimblymail-smtp' );
    }

    /**
     * Get the settings schema for this mailer as a section-shaped array.
     *
     * @since 1.0.0 Migrated to section-shaped return for SettingsProvider loop.
     *
     * @return array Section descriptor with id, title, description, help_url, and fields.
     */
    public function get_settings_fields(): array {
        return array(
            'id'          => 'resend_settings',
            'title'       => __( 'Resend', 'nimblymail-smtp' ),
            'description' => __( 'Connect to Resend email API.', 'nimblymail-smtp' ),
            'help_url'    => 'https://resend.com/api-keys',
            'fields'      => array(
                array(
                    'id'          => 'api_key',
                    'group'       => 'resend',
                    'title'       => __( 'API Key', 'nimblymail-smtp' ),
                    'type'        => 'password',
                    'required'    => true,
                    'encrypted'   => true,
                    'placeholder' => 're_xxxxxxxxx',
                ),
            ),
        );
    }

    /**
     * Configure the PHPMailer instance for this transport.
     *
     * @since 1.0.0 Drop the explicit Encryption::decrypt() call — Options::get()
     *              already decrypts sensitive fields transparently. Decrypting
     *              the already-decrypted plaintext threw a `Invalid base64 data`
     *              RuntimeException whenever this mailer was reached as the
     *              active or backup transport.
     *
     * @param PHPMailer $phpmailer The PHPMailer instance.
     *
     * @return void
     */
    public function configure( PHPMailer $phpmailer ): void {
        // Enforce SMTP engine inside PHPMailer so we can hook the transport.
        $phpmailer->isSMTP();
        $phpmailer->SMTPAutoTLS = false;

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

        // Inject our mock SMTP transport that actually calls the Resend API.
        $phpmailer->setSMTPInstance( new ResendTransport( $phpmailer, $api_key ) );
    }

    /**
     * Get this mailer's deliverability audit requirements.
     *
     * Resend signs via Amazon SES under the hood, so either include is
     * accepted as evidence of correct SPF setup.
     *
     * @since 1.0.0
     *
     * @return array{spf_includes: string[], dkim_selectors: string[], docs_url: string}
     */
    public function get_audit_requirements(): array {
        return array(
            'spf_includes'   => array( '_spf.resend.com', 'amazonses.com' ),
            'dkim_selectors' => array( 'resend' ),
            'docs_url'       => 'https://resend.com/docs/dashboard/domains/introduction',
        );
    }

    /**
     * Get the connectivity endpoints for the Resend API mailer.
     *
     * @since 1.0.0
     *
     * @return array<array<string, mixed>>
     */
    public function get_connectivity_endpoints(): array {
        return array(
            array(
                'type' => 'api',
                'url'  => 'https://api.resend.com/emails',
            ),
        );
    }
}
