<?php

declare(strict_types=1);

namespace Apexl\Io\User\Display\FormBuilders;

use Apexl\Io\Component\Collections\ComponentCollection;
use Apexl\Io\Component\Factories\GenericComponentFactory;
use Apexl\Io\FormBuilder\ComponentFactories\FormComponentFactory;
use Apexl\Io\FormBuilder\ComponentFactories\FormFieldComponentFactory;
use Apexl\Io\FormBuilder\Components\Form;
use Apexl\Io\FormBuilder\Components\FormKitField\EmailFormKitField;
use Apexl\Io\FormBuilder\FormBuilders\FormBuilder;
use Apexl\Io\Services\RouteUrls;
use Apexl\Io\Services\VariableManager;

readonly class ForgotPasswordFormBuilder extends FormBuilder
{
    public function __construct(
        private VariableManager $variableManager,
        FormFieldComponentFactory $fieldComponentFactory,
        GenericComponentFactory $componentFactory,
        RouteUrls $routeUrls,
        FormComponentFactory $formComponentFactory,
        string $formClass = Form::class
    ) {
        parent::__construct(
            $fieldComponentFactory,
            $componentFactory,
            $routeUrls,
            $formComponentFactory,
            $formClass
        );
    }

    protected function init(): void
    {
        parent::init();

        $this->form->setTitle('Enter your email to reset your password.')
            ->addClass('forgotten-password-form')
            ->setSubmitText('Get Reset Link');
    }

    protected function formAction(): string
    {
        return $this->routeUrls->url('user.forgot-password');
    }

    protected function components(ComponentCollection $components): ComponentCollection
    {
        $components->add(
            $this->field(EmailFormKitField::class, 'email')
                ->setPlaceholder(
                    $this->variableManager->get('user_login_email_placeholder') ?? 'Enter your email address'
                )
                ->setLabel('Email Address')
                ->hideLabel()
                ->isRequired()
        );

        return $components;
    }
}


