import requests
from odoo import api, SUPERUSER_ID


class CaptchaService:

    def __init__(self, env):
        self.env = env

    def verify(self, token):
        """Verify CAPTCHA token against configured provider."""
        ICP = self.env['ir.config_parameter'].sudo()
        secret_key = ICP.get_param('encoach.captcha_secret_key', '')
        provider = ICP.get_param('encoach.captcha_provider', 'recaptcha')

        if provider == 'recaptcha':
            url = 'https://www.google.com/recaptcha/api/siteverify'
        elif provider == 'hcaptcha':
            url = 'https://hcaptcha.com/siteverify'
        elif provider == 'turnstile':
            url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify'
        else:
            return False

        try:
            resp = requests.post(url, data={
                'secret': secret_key,
                'response': token,
            }, timeout=10)
            result = resp.json()
            return result.get('success', False)
        except Exception:
            return False
