<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: modules/email-postmark.php
// Description: Postmark email provider module - NUCLEAR+++
// Last Edited: 2026-05-21
// ==================================================
namespace AutoMarketerAI\Modules;
use AutoMarketerAI\AbstractEmailProvider;
if (!defined('ABSPATH')) {
    exit('No direct script access allowed.');
}
final class EmailPostmark extends AbstractEmailProvider
{
    private const API_BASE = 'https://api.postmarkapp.com';
    public function get_id(): string { return 'email-postmark'; }
    public function get_name(): string { return 'Postmark'; }
    public function get_description(): string { return 'Built for transactional email that has to arrive. Postmark separates transactional and broadcast streams, giving your critical messages the best shot at the inbox every time.'; }
    public function get_logo_url(): string { return ''; }
    public function validate_api_key(string $key): bool
    {
        if (strlen($key) < 10) { return false; }
        $response = wp_remote_get(self::API_BASE . '/server', [
            'headers' => [
                'X-Postmark-Server-Token' => $key,
                'Accept'                  => 'application/json',
            ],
            'timeout' => 10,
        ]);
        if (is_wp_error($response)) { return false; }
        $code = (int) wp_remote_retrieve_response_code($response);
        return $code === 200;
    }
    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();
        $from       = $from_name !== '' ? $from_name . ' <' . $from_email . '>' : $from_email;
        $to         = $to_name !== '' ? $to_name . ' <' . $to_email . '>' : $to_email;
        $payload = [
            'From'     => $from,
            'To'       => $to,
            'ReplyTo'  => $from_email,
            'Subject'  => $subject,
            'HtmlBody' => $html_body,
        ];
        $response = wp_remote_post(self::API_BASE . '/email', [
            'headers' => [
                'X-Postmark-Server-Token' => $api_key,
                'Content-Type'            => 'application/json',
                'Accept'                  => '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 'First 100 emails/month free. Pay-as-you-go after that, no monthly fees.'; }
    protected function get_signup_url(): string { return 'https://account.postmarkapp.com/sign_up'; }
    protected function get_signup_label(): string { return 'Create a free account'; }
}