<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: modules/email-elastic.php
// Description: Elastic Email provider module - NUCLEAR+++
// Last Edited: 2026-04-27
// ==================================================

namespace AutoMarketerAI\Modules;

use AutoMarketerAI\AbstractEmailProvider;

if (!defined('ABSPATH')) {
    exit('No direct script access allowed.');
}

final class EmailElastic extends AbstractEmailProvider
{
    private const API_BASE = 'https://api.elasticemail.com/v4';

    public function get_id(): string { return 'email-elastic'; }
    public function get_name(): string { return 'Elastic Email'; }
    public function get_description(): string { return 'Affordable, high-deliverability email platform. Handles transactional and marketing sends with list management, templates, and analytics built in.'; }
    public function get_logo_url(): string { return 'https://automarketer-ai.com/wp-content/uploads/Elastic-Email-Logo.png'; }

    public function validate_api_key(string $key): bool
    {
        if (strlen($key) < 20) { return false; }
        $response = wp_remote_get(self::API_BASE . '/contacts?limit=1', [
            'headers' => ['X-ElasticEmail-ApiKey' => $key, 'Content-Type' => 'application/json'],
            'timeout' => 10,
        ]);
        if (is_wp_error($response)) { return false; }
        $code = (int) wp_remote_retrieve_response_code($response);
        // 401/403 = bad key -- anything else (200, 400) = key is valid
        return $code !== 401 && $code !== 403;
    }

    public function send(string $to_email, string $to_name, string $subject, string $html_body, string $api_key): bool
    {
        if ($to_email === '' || $subject === '' || $html_body === '' || $api_key === '') { return false; }
        $from_name  = \AutoMarketerAI\AbstractEmailProvider::get_sender_name();
        $from_email = \AutoMarketerAI\AbstractEmailProvider::get_sender_email();
        $payload = [
            'Recipients' => ['To' => [$to_email]],
            'Content'    => [
                'Body'    => [['ContentType' => 'HTML', 'Content' => $html_body]],
                'From'    => $from_email,
                'ReplyTo' => $from_email,
                'Subject' => $subject,
            ],
            'Options' => ['SenderName' => $from_name],
        ];
        $response = wp_remote_post(self::API_BASE . '/emails/transactional', [
            'headers' => ['X-ElasticEmail-ApiKey' => $api_key, 'Content-Type' => 'application/json'],
            'body'    => (string) json_encode($payload),
            'timeout' => 15,
        ]);
        if (is_wp_error($response)) { return false; }
        $code = (int) wp_remote_retrieve_response_code($response);
        return $code >= 200 && $code < 300;
    }

    protected function get_pricing_note(): string { return 'Pay-as-you-go with no monthly minimums. Fractions of a cent per email.'; }
    protected function get_signup_url(): string { return 'https://app.elasticemail.com/api/create-account'; }
    protected function get_signup_label(): string { return 'Create a free account'; }
}
