<?php

namespace DariusMure\Captcha\View\Components;

use Illuminate\View\Component;
use DariusMure\Captcha\Services\CaptchaService;

class Captcha extends Component
{
    public string $location;
    public array $attributes;
    protected CaptchaService $captchaService;

    public function __construct(string $location = 'unknown', array $attributes = [])
    {
        $this->location = $location;
        $this->attributes = $attributes;
        $this->captchaService = app(CaptchaService::class);
    }

    public function render()
    {
        // Check if CAPTCHA is enabled for this location
        if (!$this->captchaService->isEnabled($this->location)) {
            return '';
        }

        // Check if IP is whitelisted
        if ($this->captchaService->isWhitelisted()) {
            return '';
        }

        return view('captcha::components.captcha', [
            'provider' => $this->captchaService->getProviderName(),
            'scriptUrl' => $this->captchaService->getScriptUrl(),
            'siteKey' => $this->captchaService->getSiteKey(),
            'location' => $this->location,
            'attributes' => $this->attributes,
        ]);
    }
}
