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

namespace NimblyMail\SMTP\Mailers;

use PHPMailer\PHPMailer\PHPMailer;

defined( 'ABSPATH' ) || exit;

/**
 * Class SendLayerMailer
 *
 * Implements sending via the SendLayer transactional email API.
 *
 * @since 1.0.0
 */
class SendLayerMailer extends AbstractMailer {

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

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

    /**
     * Get the mailer description shown in the mailer-cards selector.
     *
     * @since 1.0.0
     *
     * @return string
     */
    public function get_description(): string {
        return __( 'Transactional email delivery via SendLayer.', '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'          => 'sendlayer_settings',
            'title'       => __( 'SendLayer', 'nimblymail-smtp' ),
            'description' => __( 'Connect to SendLayer email API.', 'nimblymail-smtp' ),
            'help_url'    => 'https://app.sendlayer.com/settings/api',
            'fields'      => array(
                array(
                    'id'          => 'api_key',
                    'group'       => 'sendlayer',
                    'title'       => __( 'API Key', 'nimblymail-smtp' ),
                    'type'        => 'password',
                    'required'    => true,
                    'encrypted'   => true,
                    'placeholder' => 'sl_xxxxxxxxx',
                ),
            ),
        );
    }

    /**
     * Configure the PHPMailer instance for this transport.
     *
     * Sets PHPMailer to SMTP mode and injects the SendLayerTransport
     * which intercepts the send pipeline to use the SendLayer API.
     *
     * @since 1.0.0
     *
     * @param PHPMailer $phpmailer The PHPMailer instance.
     *
     * @return void
     */
    public function configure( PHPMailer $phpmailer ): void {
        $phpmailer->isSMTP();
        $phpmailer->SMTPAutoTLS = false;

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

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

    /**
     * Get this mailer's deliverability audit requirements.
     *
     * @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.sendlayer.com' ),
            'dkim_selectors' => array( 'sl' ),
            'docs_url'       => 'https://sendlayer.com/docs/authenticating-your-domain/',
        );
    }

    /**
     * Get the connectivity endpoints for the SendLayer 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.sendlayer.com/v1/emails',
            ),
        );
    }
}
