<?php
// components/Common/ClientValidation.php
namespace Components\Common;

class ClientValidation
{
    /**
     * Returns a <script> block that defines a shared client-side validator helper.
     * The helper exposes window.MBZValidator.create({ schema, getValue, getElement, isVisible })
     * and returns { clientErrors, validateField(name), validateAll() }.
     *
     * - schema: object mapping fieldName => { type, required, min, max, compare: [...] }
     * - getValue(name): fn that returns current value for a field (string or array); booleans should use 'on' / ''.
     * - getElement(name): fn that returns the input element in DOM; if not found (e.g., hidden via x-if), validation is skipped.
     * - isVisible(name): optional fn(name) => boolean; if provided and returns false, validation is skipped for that field.
     */
    public static function script(): string
    {
        $translations = [
            'required' => \mh_lng('UI_VALIDATION_REQUIRED'),
            'email' => \mh_lng('UI_VALIDATION_EMAIL'),
            'min_length' => \mh_lng('UI_VALIDATION_MIN_LENGTH'),
            'max_length' => \mh_lng('UI_VALIDATION_MAX_LENGTH'),
            'integer' => \mh_lng('UI_VALIDATION_INTEGER'),
            'min_value' => \mh_lng('UI_VALIDATION_MIN_VALUE'),
            'max_value' => \mh_lng('UI_VALIDATION_MAX_VALUE'),
            'date' => \mh_lng('UI_VALIDATION_DATE'),
            'date_time' => \mh_lng('UI_VALIDATION_DATE_TIME'),
            'date_after' => \mh_lng('UI_VALIDATION_DATE_AFTER'),
            'date_before' => \mh_lng('UI_VALIDATION_DATE_BEFORE'),
            'compare' => \mh_lng('UI_VALIDATION_COMPARE'),
        ];
        $jsonTranslations = json_encode($translations);
        ob_start();
        ?>
<script>
(function(){
  if (window.MBZValidator) return; // singleton
  window.MBZValidator = {
    translations: <?php echo $jsonTranslations; ?>,
    create: function(cfg){
      var schema = cfg && cfg.schema ? cfg.schema : {};
      var getValue = (cfg && typeof cfg.getValue === 'function') ? cfg.getValue : function(){ return ''; };
      var getElement = (cfg && typeof cfg.getElement === 'function') ? cfg.getElement : function(){ return null; };
      var isVisible = (cfg && typeof cfg.isVisible === 'function') ? cfg.isVisible : null;
      var clientErrors = {};
      function _isIntString(s){ return /^-?\d+$/.test(String(s)); }
      function _parseDateToTs(val){
        if (val === undefined || val === null) return null;
        var s = String(val).trim(); if (!s) return null;
        if (/^\d{4}-\d{2}-\d{2}$/.test(s)) { var d = new Date(s + 'T00:00:00'); if (!isNaN(d)) return Math.floor(d.getTime()/1000); }
        var t = Date.parse(s); if (!isNaN(t)) return Math.floor(t/1000);
        return null;
      }
      function _t(key, ...args) {
        var s = window.MBZValidator.translations[key] || key;
        args.forEach(function(arg) {
          s = s.replace(/%[ds]/, arg);
        });
        return s;
      }
      function validateField(name){
        var r = schema[name];
        var el = getElement(name);
        if (isVisible && !isVisible(name)) { clientErrors[name] = []; return true; }
        if (!el) { clientErrors[name] = []; return true; }
        var v = getValue(name);
        var errs = [];
        if (!r) { clientErrors[name] = []; return true; }
        if (r.required && String(v).trim() === '') { errs.push(_t('required')); }
        if (r.type === 'email' && String(v).trim() !== '') {
          var emailRe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
          if (!emailRe.test(String(v))) { errs.push(_t('email')); }
          var len = String(v).length;
          if (r.min !== undefined && _isIntString(r.min) && len < parseInt(r.min,10)) { errs.push(_t('min_length', r.min)); }
          if (r.max !== undefined && _isIntString(r.max) && len > parseInt(r.max,10)) { errs.push(_t('max_length', r.max)); }
        }
        if (r.type === 'integer' && String(v).trim() !== '') {
          if (!_isIntString(v)) { errs.push(_t('integer')); }
          var num = parseInt(v, 10);
          if (r.min !== undefined && _isIntString(r.min) && num < parseInt(r.min,10)) { errs.push(_t('min_value', r.min)); }
          if (r.max !== undefined && _isIntString(r.max) && num > parseInt(r.max,10)) { errs.push(_t('max_value', r.max)); }
        }
        if ((r.type === 'date' || r.type === 'date_time') && String(v).trim() !== '') {
          var tsv = _parseDateToTs(v);
          if (tsv === null) {
            errs.push(r.type === 'date_time' ? _t('date_time') : _t('date'));
          } else {
            if (r.min !== undefined && String(r.min).trim() !== '') {
              var tmin = _parseDateToTs(r.min);
              if (tmin !== null && tsv < tmin) { errs.push(_t('date_after', String(r.min))); }
            }
            if (r.max !== undefined && String(r.max).trim() !== '') {
              var tmax = _parseDateToTs(r.max);
              if (tmax !== null && tsv > tmax) { errs.push(_t('date_before', String(r.max))); }
            }
          }
        }
        if (Array.isArray(r.compare)){
          for (var i=0;i<r.compare.length;i++){
            var cmp = r.compare[i];
            var otherName = cmp.field; if (!otherName) continue;
            var op = (cmp.op || 'gte').toLowerCase();
            var offset = cmp.offset || 0;
            var left = v; var right = getValue(otherName);
            var compared = false;
            if (_isIntString(left) && _isIntString(right)){
              left = parseInt(left,10); right = parseInt(right,10) + (_isIntString(offset) ? parseInt(offset,10) : 0); compared = true;
            } else {
              var lTs = _parseDateToTs(left); var rTs = _parseDateToTs(right);
              if (lTs !== null && rTs !== null){ var days = _isIntString(offset) ? parseInt(offset,10) : 0; left = lTs; right = rTs + days*86400; compared = true; }
            }
            if (compared){
              var ok = true;
              switch(op){
                case 'gt': case '>': ok = (left > right); break;
                case 'gte': case '>=': ok = (left >= right); break;
                case 'lt': case '<': ok = (left < right); break;
                case 'lte': case '<=': ok = (left <= right); break;
                case 'eq': case '==': ok = (left == right); break;
                case 'ne': case '!=': ok = (left != right); break;
              }
              if (!ok){ errs.push(cmp.message || (_t('compare', op, otherName + (_isIntString(offset) ? (' + ' + offset) : '')))); }
            }
          }
        }
        clientErrors[name] = errs;
        return errs.length === 0;
      }
      function validateAll(){
        var ok = true;
        for (var name in schema){ if (!validateField(name)) ok = false; }
        return ok;
      }
      return { clientErrors: clientErrors, validateField: validateField, validateAll: validateAll };
    }
  };
})();
</script>
<?php
        return ob_get_clean();
    }
}
