<?php

namespace DariusMure\Captcha\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CaptchaSetting extends Model
{
    use HasFactory;

    protected $fillable = [
        'provider',
        'enabled',
        'site_key',
        'secret_key',
        'api_key',
        'recaptcha_v3_threshold',
        'invisible_badge_hide',
        'enabled_login',
        'enabled_register',
        'enabled_forgot_password',
        'enabled_contact',
        'enabled_api',
        'whitelist_ips',
        'testing_mode',
        'test_key',
        'fallback_provider',
        'fallback_site_key',
        'fallback_secret_key',
    ];

    protected $casts = [
        'enabled' => 'boolean',
        'recaptcha_v3_threshold' => 'decimal:2',
        'invisible_badge_hide' => 'boolean',
        'enabled_login' => 'boolean',
        'enabled_register' => 'boolean',
        'enabled_forgot_password' => 'boolean',
        'enabled_contact' => 'boolean',
        'enabled_api' => 'boolean',
        'whitelist_ips' => 'array',
        'testing_mode' => 'boolean',
    ];

    protected $attributes = [
        'enabled' => false,
        'recaptcha_v3_threshold' => 0.5,
        'invisible_badge_hide' => false,
        'enabled_login' => true,
        'enabled_register' => true,
        'enabled_forgot_password' => true,
        'enabled_contact' => false,
        'enabled_api' => false,
        'testing_mode' => false,
        'fallback_provider' => 'none',
    ];

    /**
     * Get available provider options
     */
    public static function getProviderOptions(): array
    {
        return [
            'hcaptcha' => 'hCaptcha',
            'recaptcha_v2' => 'reCAPTCHA v2 (Checkbox)',
            'recaptcha_v2_invisible' => 'reCAPTCHA v2 Invisible',
            'recaptcha_v3' => 'reCAPTCHA v3',
            'turnstile' => 'Cloudflare Turnstile',
            'trustip' => 'TrustIP',
        ];
    }

    /**
     * Get location options
     */
    public static function getLocationOptions(): array
    {
        return [
            'login' => 'Login',
            'register' => 'Register',
            'forgot_password' => 'Forgot Password',
            'contact' => 'Contact Form',
            'api' => 'API Requests',
        ];
    }

    /**
     * Check if provider needs site_key and secret_key
     */
    public function needsKeys(): bool
    {
        return $this->provider !== 'trustip';
    }

    /**
     * Check if provider needs only api_key
     */
    public function needsApiKey(): bool
    {
        return $this->provider === 'trustip';
    }

    /**
     * Check if provider supports score threshold
     */
    public function supportsThreshold(): bool
    {
        return $this->provider === 'recaptcha_v3';
    }

    /**
     * Get provider display name
     */
    public function getProviderDisplayName(): string
    {
        return self::getProviderOptions()[$this->provider] ?? $this->provider;
    }
}
