<?php
/**
 * Guard Dog Customizer Control Base Class
 *
 * Abstract base class for all customizer controls.
 *
 * @package GuardDog
 * @since 1.8.45
 */

if (!defined('ABSPATH')) exit;

abstract class Guard_Dog_Customizer_Control {

    /**
     * Control ID
     * @var string
     */
    protected $id;

    /**
     * Section ID this control belongs to
     * @var string
     */
    protected $section;

    /**
     * Control arguments
     * @var array
     */
    protected $args;

    /**
     * Constructor
     *
     * @param string $id Control ID
     * @param string $section Section ID
     * @param array $args Control arguments
     */
    public function __construct($id, $section, $args = []) {
        $this->id = $id;
        $this->section = $section;
        $this->args = wp_parse_args($args, $this->get_default_args());
    }

    /**
     * Get default arguments
     *
     * @return array
     */
    protected function get_default_args() {
        return [
            'type' => 'text',
            'label' => '',
            'description' => '',
            'default' => '',
            'priority' => 10,
            'input_attrs' => [],
        ];
    }

    /**
     * Get control ID
     *
     * @return string
     */
    public function get_id() {
        return $this->id;
    }

    /**
     * Get section ID
     *
     * @return string
     */
    public function get_section() {
        return $this->section;
    }

    /**
     * Get control argument
     *
     * @param string $key Argument key
     * @param mixed $default Default value
     * @return mixed
     */
    public function get_arg($key, $default = null) {
        return isset($this->args[$key]) ? $this->args[$key] : $default;
    }

    /**
     * Get default value
     *
     * @return mixed
     */
    public function get_default() {
        return $this->get_arg('default', '');
    }

    /**
     * Render the control
     *
     * @param mixed $value Current value
     */
    abstract public function render($value);

    /**
     * Sanitize the control value
     *
     * @param mixed $value Value to sanitize
     * @return mixed Sanitized value
     */
    abstract public function sanitize($value);

    /**
     * Validate the control value
     *
     * @param mixed $value Value to validate
     * @return bool True if valid
     */
    public function validate($value) {
        return true;
    }

    /**
     * Render control wrapper start
     */
    protected function render_wrapper_start() {
        ?>
        <div class="customizer-control" data-control-id="<?php echo esc_attr($this->id); ?>" data-control-type="<?php echo esc_attr($this->get_arg('type')); ?>">
            <?php if ($this->get_arg('label')): ?>
                <label class="customizer-control-label" for="<?php echo esc_attr($this->id); ?>">
                    <?php echo esc_html($this->get_arg('label')); ?>
                </label>
            <?php endif; ?>
        <?php
    }

    /**
     * Render control wrapper end
     */
    protected function render_wrapper_end() {
        ?>
            <?php if ($this->get_arg('description')): ?>
                <p class="description"><?php echo esc_html($this->get_arg('description')); ?></p>
            <?php endif; ?>
        </div>
        <?php
    }

    /**
     * Get input attributes as HTML string
     *
     * @return string
     */
    protected function get_input_attrs_html() {
        $attrs = $this->get_arg('input_attrs', []);
        $html = '';

        foreach ($attrs as $key => $value) {
            $html .= sprintf(' %s="%s"', esc_attr($key), esc_attr($value));
        }

        return $html;
    }
}
