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

namespace NimblyMail\SMTP\Mailers;

use PHPMailer\PHPMailer\PHPMailer;

defined( 'ABSPATH' ) || exit;

/**
 * Class SmtpMailer
 *
 * Sends email via standard SMTP.
 *
 * @since 1.0.0
 */
class SmtpMailer extends AbstractMailer {

    /**
     * {@inheritdoc}
     */
    public function get_slug(): string {
        return 'smtp';
    }

    /**
     * {@inheritdoc}
     */
    public function get_title(): string {
        return __( 'Other SMTP', 'nimblymail-smtp' );
    }

    /**
     * Get the mailer description shown in the mailer-cards selector.
     *
     * @since 1.0.0
     *
     * @return string
     */
    public function get_description(): string {
        return __( 'Connect to any SMTP server.', '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, and fields.
     */
    public function get_settings_fields(): array {
        return array(
            'id'          => 'smtp_settings',
            'title'       => __( 'SMTP Settings', 'nimblymail-smtp' ),
            'description' => __( 'Configure your SMTP server connection.', 'nimblymail-smtp' ),
            'fields'      => array(
                array(
                    'id'          => 'host',
                    'group'       => 'smtp',
                    'title'       => __( 'SMTP Host', 'nimblymail-smtp' ),
                    'type'        => 'text',
                    'required'    => true,
                    'placeholder' => 'smtp.example.com',
                ),
                array(
                    'id'       => 'port',
                    'group'    => 'smtp',
                    'title'    => __( 'SMTP Port', 'nimblymail-smtp' ),
                    'type'     => 'number',
                    'required' => true,
                ),
                array(
                    'id'      => 'encryption',
                    'group'   => 'smtp',
                    'title'   => __( 'Encryption', 'nimblymail-smtp' ),
                    'type'    => 'radio',
                    'options' => array(
                        'none' => __( 'None', 'nimblymail-smtp' ),
                        'ssl'  => __( 'SSL', 'nimblymail-smtp' ),
                        'tls'  => __( 'TLS', 'nimblymail-smtp' ),
                    ),
                ),
                array(
                    'id'    => 'autotls',
                    'group' => 'smtp',
                    'title' => __( 'Auto TLS', 'nimblymail-smtp' ),
                    'type'  => 'checkbox',
                ),
                array(
                    'id'    => 'auth',
                    'group' => 'smtp',
                    'title' => __( 'Authentication', 'nimblymail-smtp' ),
                    'type'  => 'checkbox',
                ),
                array(
                    'id'           => 'user',
                    'group'        => 'smtp',
                    'title'        => __( 'SMTP Username', 'nimblymail-smtp' ),
                    'type'         => 'text',
                    'encrypted'    => true,
                    'visible_when' => array( 'smtp.auth' => true ),
                ),
                array(
                    'id'           => 'pass',
                    'group'        => 'smtp',
                    'title'        => __( 'SMTP Password', 'nimblymail-smtp' ),
                    'type'         => 'password',
                    'encrypted'    => true,
                    'visible_when' => array( 'smtp.auth' => true ),
                ),
            ),
        );
    }

    /**
     * Configure the PHPMailer instance for SMTP transport.
     *
     * @since 1.0.0
     *
     * @param PHPMailer $phpmailer The PHPMailer instance.
     */
    public function configure( PHPMailer $phpmailer ): void {
        $phpmailer->isSMTP();
        $phpmailer->Host = $this->get_option( 'host' );
        $phpmailer->Port = (int) $this->get_option( 'port' );

        $encryption = $this->get_option( 'encryption' );
        if ( 'none' === $encryption ) {
            $phpmailer->SMTPSecure  = '';
            $phpmailer->SMTPAutoTLS = (bool) $this->get_option( 'autotls' );
        } else {
            $phpmailer->SMTPSecure = $encryption;
        }

        if ( $this->get_option( 'auth' ) ) {
            $phpmailer->SMTPAuth = true;
            $phpmailer->Username = $this->get_option( 'user' );

            $pass = $this->get_option( 'pass' );
            if ( ! empty( $pass ) ) {
                // get_option() auto-decrypts sensitive fields.
                $phpmailer->Password = $pass;
            }
        }
    }

    /**
     * Get the connectivity endpoints for the standard SMTP mailer.
     *
     * Returns the configured SMTP host and port to allow the HealthAuditor
     * to perform an outbound socket check.
     *
     * @since 1.0.0
     *
     * @return array<array<string, mixed>>
     */
    public function get_connectivity_endpoints(): array {
        $host = (string) $this->get_option( 'host' );
        $port = (int) $this->get_option( 'port' );

        if ( '' === $host || $port <= 0 ) {
            return array();
        }

        return array(
            array(
                'type' => 'smtp',
                'host' => $host,
                'port' => $port,
            ),
        );
    }
}
