<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/modules/email-resend.php
// Description: Resend email provider module - NUCLEAR+++
// Last Edited: 2026-05-26
// ==================================================

namespace AutoMarketerAI\Modules;

use AutoMarketerAI\AbstractEmailProvider;

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

final class EmailResend extends AbstractEmailProvider
{
    private const API_BASE = 'https://api.resend.com';

    public function get_id(): string { return 'email-resend'; }
    public function get_name(): string { return 'Resend'; }
    public function get_description(): string { return 'Modern email API built for developers. Clean dashboard, excellent deliverability, and a generous free tier with no credit card required.'; }
    public function get_logo_url(): string { return 'https://automarketer-ai.com/wp-content/uploads/Resend-Logo.png'; }

    public function validate_api_key(string $key): bool
    {
        if (strlen($key) < 20) { return false; }
        $response = wp_remote_get(self::API_BASE . '/domains', [
            'headers' => [
                'Authorization' => 'Bearer ' . $key,
                'Content-Type'  => '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();

        $to = $to_name !== '' ? $to_name . ' <' . $to_email . '>' : $to_email;

        $payload = [
            'from'       => $from_name . ' <' . $from_email . '>',
            'to'         => [$to],
            'subject'    => $subject,
            'html'       => $html_body,
            'reply_to'   => $from_email,
        ];

        $response = wp_remote_post(self::API_BASE . '/emails', [
            'headers' => [
                'Authorization' => 'Bearer ' . $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 '3,000 emails/month free. Paid plans from $20/month for higher volume.'; }
    protected function get_signup_url(): string { return 'https://resend.com/signup'; }
    protected function get_signup_label(): string { return 'Create a free account'; }
}