<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: modules/email-sendgrid.php
// Description: SendGrid provider module - NUCLEAR+++
// Last Edited: 2026-05-10
// ==================================================

namespace AutoMarketerAI\Modules;

use AutoMarketerAI\AbstractEmailProvider;

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

final class EmailSendgrid extends AbstractEmailProvider
{
    private const API_BASE = 'https://api.sendgrid.com/v3';

    public function get_id(): string { return 'email-sendgrid'; }
    public function get_name(): string { return 'SendGrid'; }
    public function get_description(): string { return 'Industry-standard transactional email platform. Reliable delivery, detailed analytics, and a generous free tier for getting started.'; }
    public function get_logo_url(): string { return 'https://automarketer-ai.com/wp-content/uploads/sendgrid-logo.png'; }

    public function validate_api_key(string $key): bool
    {
        if (strlen($key) < 20) { return false; }
        $response = wp_remote_get(self::API_BASE . '/scopes', [
            '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 !== 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 = [
            'personalizations' => [
                [
                    'to' => [['email' => $to_email]],
                ],
            ],
            'from'    => ['email' => $from_email, 'name' => $from_name],
            'subject' => $subject,
            'content' => [
                ['type' => 'text/html', 'value' => $html_body],
            ],
        ];
        $response = wp_remote_post(self::API_BASE . '/mail/send', [
            '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);
        // SendGrid returns 202 Accepted on success
        return $code >= 200 && $code < 300;
    }

    protected function get_pricing_note(): string { return 'Free tier includes 100 emails/day. Paid plans scale from there with no setup fees.'; }
    protected function get_signup_url(): string { return 'https://signup.sendgrid.com/'; }
    protected function get_signup_label(): string { return 'Create a free account'; }
}