<?php

namespace GriffinForms\Includes\Integrations\FormControls;

/**
 * Checkbox Form Control
 *
 * Renders a checkbox field for integration configuration forms.
 * Useful for boolean toggle options like "Enable feature X".
 *
 * @since 1.0.0
 */
class Checkbox extends Format
{
    /**
     * Get the checkbox label (appears next to the checkbox)
     *
     * @return string The checkbox label
     */
    protected function getCheckboxLabel(): string
    {
        return $this->field['checkbox_label'] ?? $this->getLabel();
    }

    /**
     * Render the checkbox field
     *
     * @return string The HTML markup
     */
    public function render(): string
    {
        $html_id = $this->getHtmlId();
        $field_name = $this->getFieldName();
        $checkbox_label = $this->getCheckboxLabel();

        // Determine if checkbox is checked
        $is_checked = !empty($this->field_value);

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

        // Render checkbox with label using Bootstrap form-check pattern
        $html .= '<div class="form-check d-flex align-items-center">';

        $html .= sprintf(
            '<input type="checkbox" class="form-check-input gf-integration-control-checkbox" id="%s" name="%s" value="1" %s />',
            esc_attr($html_id),
            esc_attr($field_name),
            $is_checked ? 'checked="checked"' : ''
        );

        $html .= sprintf(
            '<label class="form-check-label gf-integration-checkbox-label" for="%s">%s</label>',
            esc_attr($html_id),
            esc_html($checkbox_label)
        );

        $html .= '</div>';

        // Render description
        $description = $this->renderDescription();
        if (!empty($description)) {
            $html .= $description;
        }

        $html .= '</div>';

        return $html;
    }
}
