<?php

namespace GriffinForms\Includes\Integrations\FormControls;

/**
 * Text Input Form Control
 *
 * Renders a text input field for integration configuration forms.
 * Supports regular text, email, and password input types.
 *
 * @since 1.0.0
 */
class Text extends Format
{
    /**
     * Get the input type
     *
     * @return string The input type (text, email, password, etc.)
     */
    protected function getInputType(): string
    {
        return $this->field['type'] ?? 'text';
    }

    /**
     * Get the placeholder text
     *
     * @return string The placeholder
     */
    protected function getPlaceholder(): string
    {
        return $this->field['placeholder'] ?? '';
    }

    /**
     * Render the text input field
     *
     * @return string The HTML markup
     */
    public function render(): string
    {
        $html_id = $this->getHtmlId();
        $field_name = $this->getFieldName();
        $input_type = $this->getInputType();
        $placeholder = $this->getPlaceholder();

        $html = sprintf(
            '<div class="%s">',
            esc_attr($this->getContainerClasses())
        );

        // Render label
        $html .= $this->renderLabel();

        // Render input
        $input_attrs = [
            'type' => esc_attr($input_type),
            'id' => esc_attr($html_id),
            'name' => esc_attr($field_name),
            'class' => esc_attr($this->getInputClasses()),
            'value' => $this->escapeAttr($this->field_value),
        ];

        // Add placeholder if provided
        if (!empty($placeholder)) {
            $input_attrs['placeholder'] = esc_attr($placeholder);
        }

        // Add required attribute if needed
        if ($this->isRequired()) {
            $input_attrs['required'] = 'required';
        }

        // Build input element
        $html .= '<input';
        foreach ($input_attrs as $attr_name => $attr_value) {
            $html .= sprintf(' %s="%s"', $attr_name, $attr_value);
        }
        $html .= ' />';

        // Render description
        $html .= $this->renderDescription();

        $html .= '</div>';

        return $html;
    }
}
