<?php

namespace GriffinForms\Includes\Integrations\Categories;

use GriffinForms\Includes\Integrations\Categories\Format;

/**
 * Registry for CAPTCHA providers.
 */
class Captcha extends Format
{
    protected $category_key = 'captcha';
    protected $active_option = 'active_captcha_provider';

    public function getConfiguredProviders(): array
    {
        $configured = [];
        foreach ($this->provider_cache as $id => $provider) {
            $integration = $this->providers_service->getIntegration($id);
            if ($integration && $integration->isConfigured()) {
                $configured[$id] = $provider;
            }
        }

        return $configured;
    }

    public function getActiveProviderId(): ?string
    {
        $selected = sanitize_key($this->settings->getOption($this->active_option));
        if ($selected === 'none') {
            return null;
        }

        if ($selected && isset($this->provider_cache[$selected]) && $this->isProviderConfigured($selected)) {
            return $selected;
        }

        foreach ($this->getConfiguredProviders() as $id => $provider) {
            return $id;
        }

        return array_key_first($this->provider_cache) ?: null;
    }

    public function getActiveIntegration(int $form_id = 0)
    {
        $active_id = $this->getActiveProviderId();
        if (!$active_id) {
            return null;
        }

        $integration = $this->providers_service->getIntegration($active_id);
        if ($integration && method_exists($integration, 'setFormContextId')) {
            $integration->setFormContextId($form_id);
        }

        return $integration;
    }

    public function getProviderOptions(): array
    {
        $options = [];
        foreach ($this->provider_cache as $id => $provider) {
            $options[] = [
                'id'         => $id,
                'label'      => $provider['name'] ?? $id,
                'configured' => $this->isProviderConfigured($id),
            ];
        }

        return $options;
    }

    public function saveActiveProvider(string $provider_id): bool
    {
        if ($provider_id === 'none') {
            $this->settings->updateOption($this->active_option, 'none', 'antispam');
            return true;
        }

        if (!isset($this->provider_cache[$provider_id])) {
            return false;
        }

        $this->settings->updateOption($this->active_option, sanitize_key($provider_id), 'antispam');
        return true;
    }

    public function ensureActiveProvider(string $provider_id): void
    {
        $current_raw = sanitize_key($this->settings->getOption($this->active_option));

        if ($current_raw === 'none') {
            return;
        }

        if (empty($current_raw) || !$this->isProviderConfigured($current_raw)) {
            $this->saveActiveProvider($provider_id);
        }
    }

    private function isProviderConfigured(string $provider_id): bool
    {
        $integration = $this->providers_service->getIntegration($provider_id);
        return $integration ? (bool) $integration->isConfigured() : false;
    }
}
