<?php
namespace Storzen\Fields;
use Storzen\Contracts\Field;
defined('ABSPATH') || exit;

class Number extends Field {
    /** @var int|float */ protected $min  = 0;
    /** @var int|float */ protected $max  = 9999;
    /** @var int|float */ protected $step = 1;

    /** @return static */ public function set_min( $v )  { $this->min  = $v; return $this; }
    /** @return static */ public function set_max( $v )  { $this->max  = $v; return $this; }
    /** @return static */ public function set_step( $v ) { $this->step = $v; return $this; }
    /** @return static */ public function min( $v )      { return $this->set_min($v);  }
    /** @return static */ public function max( $v )      { return $this->set_max($v);  }
    /** @return static */ public function step( $v )     { return $this->set_step($v); }

    public function sanitize( $raw ) {
        return max( $this->min, min( $this->max, (float)$raw ) );
    }
    public function render( $value ) {
    $id = $this->field_id();
    $is_pro_active = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
    $show_pro = $this->pro && ! $is_pro_active;
    echo '<input type="number" class="sz-input sz-input--number"';
    echo ' id="' . esc_attr($id) . '" name="' . esc_attr($this->field_name()) . '"';
    echo ' value="' . esc_attr( (string)$value ) . '"';
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- disabled attribute may be added
    echo ' min="' . esc_attr($this->min) . '" max="' . esc_attr($this->max) . '" step="' . esc_attr($this->step) . '"' . ( $show_pro ? ' disabled' : '' ) . '>';
    if ( $show_pro ) echo '<span class="sz-pro-badge">PRO</span>';
    }
}
