<?php

namespace App\GooglePlugins;

use App\Models\GooglePlugin;
use App\GooglePlugins\Contracts\BaseGooglePlugin;

class CloudflareTurnstile extends BaseGooglePlugin
{
    public function getCode(): string
    {
        return 'cloudflare_turnstile';
    }

    public function getName(): string
    {
        return 'Cloudflare Turnstile';
    }

    public function getDescription(): string
    {
        return 'Cloudflare Turnstile — a privacy-focused CAPTCHA alternative. Protects forms without requiring user interaction.';
    }

    public function getConfigFields(): array
    {
        return [
            [
                'key' => 'site_key',
                'label' => 'Site Key',
                'type' => 'text',
                'placeholder' => '0x4AAAAAAxxxxxxxxxxxxxxxxxxx',
                'required' => true,
            ],
            [
                'key' => 'secret_key',
                'label' => 'Secret Key',
                'type' => 'password',
                'placeholder' => '0x4AAAAAAxxxxxxxxxxxxxxxxxxx',
                'required' => true,
            ],
        ];
    }

    public function validateConfig(array $config): array
    {
        return [
            'site_key' => trim($config['site_key'] ?? ''),
            'secret_key' => trim($config['secret_key'] ?? ''),
        ];
    }

    public function isConfigured(GooglePlugin $plugin): bool
    {
        $config = $plugin->config ?? [];
        return !empty($config['site_key']) && !empty($config['secret_key']);
    }

    public function renderHead(GooglePlugin $plugin): string
    {
        $key = $plugin->config['site_key'] ?? '';
        if (!$key) return '';

        return <<<HTML
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
HTML;
    }

    public function renderBody(GooglePlugin $plugin): string
    {
        return '';
    }
}
