<?php

namespace GriffinForms\Includes\Integrations\FormControls;

/**
 * Form Control Base Class
 *
 * Provides common functionality for all form control classes used in
 * integration configuration forms.
 *
 * @since 1.0.0
 */
abstract class Format
{
    /**
     * Field configuration array
     *
     * @var array
     */
    protected $field = [];

    /**
     * Field key/identifier
     *
     * @var string
     */
    protected $field_key = '';

    /**
     * Field value
     *
     * @var mixed
     */
    protected $field_value = '';

    /**
     * Integration ID
     *
     * @var string
     */
    protected $integration_id = '';

    /**
     * Constructor
     *
     * @param string $field_key The unique identifier for this field
     * @param mixed $field_value The current value of the field
     * @param string $integration_id The integration ID (e.g., 'recaptcha')
     * @param array $field_config Optional field configuration array
     */
    public function __construct(string $field_key, $field_value = '', string $integration_id = '', array $field_config = [])
    {
        $this->field_key = $field_key;
        $this->field_value = $field_value;
        $this->integration_id = $integration_id;
        $this->field = $field_config;
    }

    /**
     * Get the HTML ID for this form control
     *
     * @return string The HTML ID
     */
    protected function getHtmlId(): string
    {
        return sprintf('gf-%s-%s', $this->integration_id, $this->field_key);
    }

    /**
     * Get the name attribute for form input
     *
     * @return string The form field name
     */
    protected function getFieldName(): string
    {
        return $this->field_key;
    }

    /**
     * Get the label for this field
     *
     * @return string The field label
     */
    protected function getLabel(): string
    {
        return $this->field['label'] ?? ucfirst(str_replace('_', ' ', $this->field_key));
    }

    /**
     * Check if this field is required
     *
     * @return bool True if field is required
     */
    protected function isRequired(): bool
    {
        return $this->field['required'] ?? false;
    }

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

    /**
     * Get CSS classes for the form control container
     *
     * @return string Space-separated CSS classes
     */
    protected function getContainerClasses(): string
    {
        $classes = ['mb-3', 'gf-integration-control'];
        $extra = $this->field['container_class'] ?? '';
        if (is_string($extra) && trim($extra) !== '') {
            $classes[] = trim($extra);
        }

        return implode(' ', array_filter($classes));
    }

    /**
     * Get CSS classes for the label element
     *
     * @return string Space-separated CSS classes
     */
    protected function getLabelClasses(): string
    {
        return 'form-label gf-integration-control-label';
    }

    /**
     * Get CSS classes for the input element
     *
     * @return string Space-separated CSS classes
     */
    protected function getInputClasses(): string
    {
        $classes = ['form-control', 'gf-integration-control-input'];
        $extra = $this->field['input_class'] ?? '';
        if (is_string($extra) && trim($extra) !== '') {
            $classes[] = trim($extra);
        }

        return implode(' ', array_filter($classes));
    }

    /**
     * Render the form control HTML
     *
     * Must be implemented by child classes
     *
     * @return string The HTML markup
     */
    abstract public function render(): string;

    /**
     * Render the label element
     *
     * @return string The label HTML
     */
    protected function renderLabel(): string
    {
        $label = $this->getLabel();
        $html_id = $this->getHtmlId();

        $required_mark = '';
        if ($this->isRequired()) {
            $required_mark = '<span class="text-danger">*</span>';
        }

        return sprintf(
            '<label for="%s" class="%s">%s %s</label>',
            esc_attr($html_id),
            esc_attr($this->getLabelClasses()),
            esc_html($label),
            $required_mark
        );
    }

    /**
     * Render the help text/description
     *
     * @return string The help text HTML
     */
    protected function renderDescription(): string
    {
        $description = $this->getDescription();
        if (empty($description)) {
            return '';
        }

        return sprintf(
            '<div class="form-text gf-integration-control-description">%s</div>',
            esc_html($description)
        );
    }

    /**
     * Escape attribute values
     *
     * @param mixed $value The value to escape
     * @return string The escaped value
     */
    protected function escapeAttr($value): string
    {
        return esc_attr((string) $value);
    }

    /**
     * Escape HTML
     *
     * @param mixed $value The value to escape
     * @return string The escaped HTML
     */
    protected function escapeHtml($value): string
    {
        return esc_html((string) $value);
    }
}
