<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: modules/ai-claude.php
// Last Edited: 2026-04-21
// Description: Claude provider module — NUCLEAR+++
// ==================================================

namespace AutoMarketerAI\Modules;

use AutoMarketerAI\AbstractAiProvider;

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

final class Claude extends AbstractAiProvider
{
    // Current model used for key validation — must be an active Anthropic model
    private const VALIDATION_MODEL = 'claude-haiku-4-5';

    // Default model pre-selected in the activation modal
    private const DEFAULT_MODEL = 'claude-sonnet-4-6';

    public function get_id(): string
    {
        return 'ai-claude';
    }

    public function get_name(): string
    {
        return 'Claude';
    }

    public function get_description(): string
    {
        return 'Intelligent, safe, and helpful AI from Anthropic. Excellent for reasoning, coding, analysis, and long-context tasks.';
    }

    public function get_logo_url(): string
    {
        return '';
    }

    public function get_model_count(): int
    {
        return 3;
    }

    public function get_available_models(): array
    {
        return [
            'claude-opus-4-6'   => 'Claude Opus 4.6 — Most powerful',
            'claude-sonnet-4-6' => 'Claude Sonnet 4.6 — Best overall',
            'claude-haiku-4-5'  => 'Claude Haiku 4.5 — Fast & efficient',
        ];
    }

    public function get_model_descriptions(): array
    {
        return [
            'claude-opus-4-6'   => 'Most intelligent Claude model. Best for complex reasoning, nuanced writing, and long-context tasks.',
            'claude-sonnet-4-6' => 'The best balance of speed and quality. Recommended for most marketing content generation.',
            'claude-haiku-4-5'  => 'Fastest and most cost-efficient. Great for high-volume, straightforward content tasks.',
        ];
    }

    public function get_default_model(): string
    {
        return self::DEFAULT_MODEL;
    }

    public function get_api_key_prefix(): string
    {
        return 'sk-ant-';
    }

    public function validate_api_key(string $key): bool
    {
        if (!str_starts_with($key, 'sk-ant-')) {
            return false;
        }

        $response = wp_remote_post('https://api.anthropic.com/v1/messages', [
            'headers' => [
                'x-api-key'         => $key,
                'anthropic-version' => '2023-06-01',
                'Content-Type'      => 'application/json',
            ],
            'body'    => (string) json_encode([
                'model'      => self::VALIDATION_MODEL,
                'max_tokens' => 1,
                'messages'   => [['role' => 'user', 'content' => 'hi']],
            ]),
            'timeout' => 10,
        ]);

        if (is_wp_error($response)) {
            return false;
        }

        $code = wp_remote_retrieve_response_code($response);
        return $code === 200 || $code === 400;
    }

    public function generate_raw(string $prompt, string $api_key, string $model, int $max_tokens = 64): string
    {
        if ($prompt === '' || $api_key === '' || $model === '') {
            return '';
        }

        $response = wp_remote_post('https://api.anthropic.com/v1/messages', [
            'headers' => [
                'x-api-key'         => $api_key,
                'anthropic-version' => '2023-06-01',
                'Content-Type'      => 'application/json',
            ],
            'body'    => (string) json_encode([
                'model'      => $model,
                'max_tokens' => $max_tokens,
                'messages'   => [['role' => 'user', 'content' => $prompt]],
            ]),
            'timeout' => 15,
        ]);

        if (is_wp_error($response)) {
            return '';
        }

        $http_code = (int) wp_remote_retrieve_response_code($response);
        if ($http_code !== 200) {
            if ($http_code === 401 || $http_code === 429) {
                set_transient('amai_provider_credit_warning', $this->get_id(), HOUR_IN_SECONDS);
            }
            return '';
        }

        $body = json_decode(wp_remote_retrieve_body($response), true);
        return trim((string) ($body['content'][0]['text'] ?? ''));
    }

    protected function get_pricing_note(): string
    {
        return 'Anthropic API is pay-as-you-go - start with a small deposit.';
    }

    protected function get_signup_url(): string
    {
        return 'https://console.anthropic.com/signup';
    }

    protected function get_signup_label(): string
    {
        return 'Get one in 5 minutes';
    }
}