const ValidationUtils = require('../src/helper/validationUtils');
const { validateNumber, validateInteger, validateBoolean, validateString, validateEnum } = require('../src/helper/validators/typeValidators');
const { validateArray, validateSet, validateObject } = require('../src/helper/validators/collectionValidators');
const { validateCurve, validateMachineCurve, isSorted, isUnique, areNumbers } = require('../src/helper/validators/curveValidator');

// Shared mock logger used across tests
function mockLogger() {
  return { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() };
}

// ═══════════════════════════════════════════════════════════════════════
// Type validators
// ═══════════════════════════════════════════════════════════════════════
describe('typeValidators', () => {
  let logger;
  beforeEach(() => { logger = mockLogger(); });

  // ── validateNumber ──────────────────────────────────────────────────
  describe('validateNumber()', () => {
    it('should accept a valid number', () => {
      expect(validateNumber(42, {}, { default: 0 }, 'n', 'k', logger)).toBe(42);
    });

    it('should parse a string to a number', () => {
      expect(validateNumber('3.14', {}, { default: 0 }, 'n', 'k', logger)).toBe(3.14);
      expect(logger.warn).toHaveBeenCalled();
    });

    it('should return default when below min', () => {
      expect(validateNumber(1, { min: 5 }, { default: 5 }, 'n', 'k', logger)).toBe(5);
    });

    it('should return default when above max', () => {
      expect(validateNumber(100, { max: 50 }, { default: 50 }, 'n', 'k', logger)).toBe(50);
    });

    it('should accept boundary value equal to min', () => {
      expect(validateNumber(5, { min: 5 }, { default: 0 }, 'n', 'k', logger)).toBe(5);
    });

    it('should accept boundary value equal to max', () => {
      expect(validateNumber(50, { max: 50 }, { default: 0 }, 'n', 'k', logger)).toBe(50);
    });
  });

  // ── validateInteger ─────────────────────────────────────────────────
  describe('validateInteger()', () => {
    it('should accept a valid integer', () => {
      expect(validateInteger(7, {}, { default: 0 }, 'n', 'k', logger)).toBe(7);
    });

    it('should parse a string to an integer', () => {
      expect(validateInteger('10', {}, { default: 0 }, 'n', 'k', logger)).toBe(10);
    });

    it('should return default for a non-parseable value', () => {
      expect(validateInteger('abc', {}, { default: -1 }, 'n', 'k', logger)).toBe(-1);
    });

    it('should return default when below min', () => {
      expect(validateInteger(2, { min: 5 }, { default: 5 }, 'n', 'k', logger)).toBe(5);
    });

    it('should return default when above max', () => {
      expect(validateInteger(100, { max: 50 }, { default: 50 }, 'n', 'k', logger)).toBe(50);
    });

    it('should parse a float string and truncate to integer', () => {
      expect(validateInteger('7.9', {}, { default: 0 }, 'n', 'k', logger)).toBe(7);
    });
  });

  // ── validateBoolean ─────────────────────────────────────────────────
  describe('validateBoolean()', () => {
    it('should pass through a true boolean', () => {
      expect(validateBoolean(true, 'n', 'k', logger)).toBe(true);
    });

    it('should pass through a false boolean', () => {
      expect(validateBoolean(false, 'n', 'k', logger)).toBe(false);
    });

    it('should parse string "true" to boolean true', () => {
      expect(validateBoolean('true', 'n', 'k', logger)).toBe(true);
    });

    it('should parse string "false" to boolean false', () => {
      expect(validateBoolean('false', 'n', 'k', logger)).toBe(false);
    });

    it('should pass through non-boolean non-string values unchanged', () => {
      expect(validateBoolean(42, 'n', 'k', logger)).toBe(42);
    });
  });

  // ── validateString ──────────────────────────────────────────────────
  describe('validateString()', () => {
    it('should accept a lowercase string', () => {
      expect(validateString('hello', {}, { default: '' }, 'n', 'k', logger)).toBe('hello');
    });

    it('should convert uppercase to lowercase', () => {
      expect(validateString('Hello', {}, { default: '' }, 'n', 'k', logger)).toBe('hello');
    });

    it('should convert a number to a string', () => {
      expect(validateString(42, {}, { default: '' }, 'n', 'k', logger)).toBe('42');
    });

    it('should return null when nullable and value is null', () => {
      expect(validateString(null, { nullable: true }, { default: '' }, 'n', 'k', logger)).toBeNull();
    });
  });

  // ── validateEnum ────────────────────────────────────────────────────
  describe('validateEnum()', () => {
    const rules = { values: [{ value: 'open' }, { value: 'closed' }, { value: 'partial' }] };

    it('should accept a valid enum value', () => {
      expect(validateEnum('open', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('open');
    });

    it('should be case-insensitive', () => {
      expect(validateEnum('OPEN', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('open');
    });

    it('should return default for an invalid value', () => {
      expect(validateEnum('invalid', rules, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
    });

    it('should return default when value is null', () => {
      expect(validateEnum(null, rules, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
    });

    it('should return default when rules.values is not an array', () => {
      expect(validateEnum('open', {}, { default: 'closed' }, 'n', 'k', logger)).toBe('closed');
    });
  });
});

// ═══════════════════════════════════════════════════════════════════════
// Collection validators
// ═══════════════════════════════════════════════════════════════════════
describe('collectionValidators', () => {
  let logger;
  beforeEach(() => { logger = mockLogger(); });

  // ── validateArray ───────────────────────────────────────────────────
  describe('validateArray()', () => {
    it('should return default when value is not an array', () => {
      expect(validateArray('not-array', { itemType: 'number' }, { default: [1] }, 'n', 'k', logger))
        .toEqual([1]);
    });

    it('should filter items by itemType', () => {
      const result = validateArray([1, 'a', 2], { itemType: 'number', minLength: 1 }, { default: [] }, 'n', 'k', logger);
      expect(result).toEqual([1, 2]);
    });

    it('should respect maxLength', () => {
      const result = validateArray([1, 2, 3, 4, 5], { itemType: 'number', maxLength: 3, minLength: 1 }, { default: [] }, 'n', 'k', logger);
      expect(result).toEqual([1, 2, 3]);
    });

    it('should return default when fewer items than minLength after filtering', () => {
      const result = validateArray(['a'], { itemType: 'number', minLength: 1 }, { default: [0] }, 'n', 'k', logger);
      expect(result).toEqual([0]);
    });

    it('should pass all items through when itemType is null', () => {
      const result = validateArray([1, 'a', true], { itemType: 'null', minLength: 1 }, { default: [] }, 'n', 'k', logger);
      expect(result).toEqual([1, 'a', true]);
    });
  });

  // ── validateSet ─────────────────────────────────────────────────────
  describe('validateSet()', () => {
    it('should convert default to Set when value is not a Set', () => {
      const result = validateSet('not-a-set', { itemType: 'number' }, { default: [1, 2] }, 'n', 'k', logger);
      expect(result).toBeInstanceOf(Set);
      expect([...result]).toEqual([1, 2]);
    });

    it('should filter Set items by type', () => {
      const input = new Set([1, 'a', 2]);
      const result = validateSet(input, { itemType: 'number', minLength: 1 }, { default: [] }, 'n', 'k', logger);
      expect([...result]).toEqual([1, 2]);
    });

    it('should return default Set when too few items remain', () => {
      const input = new Set(['a']);
      const result = validateSet(input, { itemType: 'number', minLength: 1 }, { default: [0] }, 'n', 'k', logger);
      expect([...result]).toEqual([0]);
    });
  });

  // ── validateObject ──────────────────────────────────────────────────
  describe('validateObject()', () => {
    it('should return default when value is not an object', () => {
      expect(validateObject('str', {}, { default: { a: 1 } }, 'n', 'k', jest.fn(), logger))
        .toEqual({ a: 1 });
    });

    it('should return default when value is an array', () => {
      expect(validateObject([1, 2], {}, { default: {} }, 'n', 'k', jest.fn(), logger))
        .toEqual({});
    });

    it('should return default when no schema is provided', () => {
      expect(validateObject({ a: 1 }, {}, { default: { b: 2 } }, 'n', 'k', jest.fn(), logger))
        .toEqual({ b: 2 });
    });

    it('should call validateSchemaFn when schema is provided', () => {
      const mockFn = jest.fn().mockReturnValue({ validated: true });
      const rules = { schema: { x: { default: 1 } } };
      const result = validateObject({ x: 2 }, rules, {}, 'n', 'k', mockFn, logger);
      expect(mockFn).toHaveBeenCalledWith({ x: 2 }, rules.schema, 'n.k');
      expect(result).toEqual({ validated: true });
    });
  });
});

// ═══════════════════════════════════════════════════════════════════════
// Curve validators
// ═══════════════════════════════════════════════════════════════════════
describe('curveValidator', () => {
  let logger;
  beforeEach(() => { logger = mockLogger(); });

  // ── Helper utilities ────────────────────────────────────────────────
  describe('isSorted()', () => {
    it('should return true for a sorted array', () => {
      expect(isSorted([1, 2, 3, 4])).toBe(true);
    });

    it('should return false for an unsorted array', () => {
      expect(isSorted([3, 1, 2])).toBe(false);
    });

    it('should return true for an empty array', () => {
      expect(isSorted([])).toBe(true);
    });

    it('should return true for equal adjacent values', () => {
      expect(isSorted([1, 1, 2])).toBe(true);
    });
  });

  describe('isUnique()', () => {
    it('should return true when all values are unique', () => {
      expect(isUnique([1, 2, 3])).toBe(true);
    });

    it('should return false when duplicates exist', () => {
      expect(isUnique([1, 2, 2])).toBe(false);
    });
  });

  describe('areNumbers()', () => {
    it('should return true for all numbers', () => {
      expect(areNumbers([1, 2.5, -3])).toBe(true);
    });

    it('should return false when a non-number is present', () => {
      expect(areNumbers([1, 'a', 3])).toBe(false);
    });
  });

  // ── validateCurve ───────────────────────────────────────────────────
  describe('validateCurve()', () => {
    const defaultCurve = { line1: { x: [0, 1], y: [0, 1] } };

    it('should return default when input is null', () => {
      expect(validateCurve(null, defaultCurve, logger)).toEqual(defaultCurve);
    });

    it('should return default for an empty object', () => {
      expect(validateCurve({}, defaultCurve, logger)).toEqual(defaultCurve);
    });

    it('should validate a correct curve', () => {
      const curve = { line1: { x: [1, 2, 3], y: [10, 20, 30] } };
      const result = validateCurve(curve, defaultCurve, logger);
      expect(result.line1.x).toEqual([1, 2, 3]);
      expect(result.line1.y).toEqual([10, 20, 30]);
    });

    it('should sort unsorted x values and reorder y accordingly', () => {
      const curve = { line1: { x: [3, 1, 2], y: [30, 10, 20] } };
      const result = validateCurve(curve, defaultCurve, logger);
      expect(result.line1.x).toEqual([1, 2, 3]);
      expect(result.line1.y).toEqual([10, 20, 30]);
    });

    it('should remove duplicate x values', () => {
      const curve = { line1: { x: [1, 1, 2], y: [10, 11, 20] } };
      const result = validateCurve(curve, defaultCurve, logger);
      expect(result.line1.x).toEqual([1, 2]);
      expect(result.line1.y.length).toBe(2);
    });

    it('should return default when y contains non-numbers', () => {
      const curve = { line1: { x: [1, 2], y: ['a', 'b'] } };
      expect(validateCurve(curve, defaultCurve, logger)).toEqual(defaultCurve);
    });
  });

  // ── validateMachineCurve ────────────────────────────────────────────
  describe('validateMachineCurve()', () => {
    const defaultMC = {
      nq: { line1: { x: [0, 1], y: [0, 1] } },
      np: { line1: { x: [0, 1], y: [0, 1] } },
    };

    it('should return default when input is null', () => {
      expect(validateMachineCurve(null, defaultMC, logger)).toEqual(defaultMC);
    });

    it('should return default when nq or np is missing', () => {
      expect(validateMachineCurve({ nq: {} }, defaultMC, logger)).toEqual(defaultMC);
    });

    it('should validate a correct machine curve', () => {
      const input = {
        nq: { line1: { x: [1, 2], y: [10, 20] } },
        np: { line1: { x: [1, 2], y: [5, 10] } },
      };
      const result = validateMachineCurve(input, defaultMC, logger);
      expect(result.nq.line1.x).toEqual([1, 2]);
      expect(result.np.line1.y).toEqual([5, 10]);
    });
  });
});

// ═══════════════════════════════════════════════════════════════════════
// ValidationUtils class
// ═══════════════════════════════════════════════════════════════════════
describe('ValidationUtils', () => {
  let vu;

  beforeEach(() => {
    vu = new ValidationUtils(true, 'error'); // suppress most logging noise
  });

  // ── constrain() ─────────────────────────────────────────────────────
  describe('constrain()', () => {
    it('should return value when within range', () => {
      expect(vu.constrain(5, 0, 10)).toBe(5);
    });

    it('should clamp to min when value is below range', () => {
      expect(vu.constrain(-5, 0, 10)).toBe(0);
    });

    it('should clamp to max when value is above range', () => {
      expect(vu.constrain(15, 0, 10)).toBe(10);
    });

    it('should return min for boundary value equal to min', () => {
      expect(vu.constrain(0, 0, 10)).toBe(0);
    });

    it('should return max for boundary value equal to max', () => {
      expect(vu.constrain(10, 0, 10)).toBe(10);
    });

    it('should return min when value is not a number', () => {
      expect(vu.constrain('abc', 0, 10)).toBe(0);
    });

    it('should return min when value is null', () => {
      expect(vu.constrain(null, 0, 10)).toBe(0);
    });

    it('should return min when value is undefined', () => {
      expect(vu.constrain(undefined, 0, 10)).toBe(0);
    });
  });

  // ── validateSchema() ────────────────────────────────────────────────
  describe('validateSchema()', () => {
    it('should use default value when config key is missing', () => {
      const schema = {
        speed: { default: 100, rules: { type: 'number' } },
      };
      const result = vu.validateSchema({}, schema, 'test');
      expect(result.speed).toBe(100);
    });

    it('should use provided value over default', () => {
      const schema = {
        speed: { default: 100, rules: { type: 'number' } },
      };
      const result = vu.validateSchema({ speed: 200 }, schema, 'test');
      expect(result.speed).toBe(200);
    });

    it('should strip unknown keys from config', () => {
      const schema = {
        speed: { default: 100, rules: { type: 'number' } },
      };
      const config = { speed: 50, unknownKey: 'bad' };
      const result = vu.validateSchema(config, schema, 'test');
      expect(result.unknownKey).toBeUndefined();
      expect(result.speed).toBe(50);
    });

    it('should validate number type with min/max', () => {
      const schema = {
        speed: { default: 10, rules: { type: 'number', min: 0, max: 100 } },
      };
      // within range
      expect(vu.validateSchema({ speed: 50 }, schema, 'test').speed).toBe(50);
      // below min -> default
      expect(vu.validateSchema({ speed: -1 }, schema, 'test').speed).toBe(10);
      // above max -> default
      expect(vu.validateSchema({ speed: 101 }, schema, 'test').speed).toBe(10);
    });

    it('should validate boolean type', () => {
      const schema = {
        enabled: { default: true, rules: { type: 'boolean' } },
      };
      expect(vu.validateSchema({ enabled: false }, schema, 'test').enabled).toBe(false);
      expect(vu.validateSchema({ enabled: 'true' }, schema, 'test').enabled).toBe(true);
    });

    it('should validate string type (lowercased)', () => {
      const schema = {
        mode: { default: 'auto', rules: { type: 'string' } },
      };
      expect(vu.validateSchema({ mode: 'Manual' }, schema, 'test').mode).toBe('manual');
    });

    it('should validate enum type', () => {
      const schema = {
        state: {
          default: 'open',
          rules: { type: 'enum', values: [{ value: 'open' }, { value: 'closed' }] },
        },
      };
      expect(vu.validateSchema({ state: 'closed' }, schema, 'test').state).toBe('closed');
      expect(vu.validateSchema({ state: 'invalid' }, schema, 'test').state).toBe('open');
    });

    it('should validate integer type', () => {
      const schema = {
        count: { default: 5, rules: { type: 'integer', min: 1, max: 100 } },
      };
      expect(vu.validateSchema({ count: 10 }, schema, 'test').count).toBe(10);
      expect(vu.validateSchema({ count: '42' }, schema, 'test').count).toBe(42);
    });

    it('should validate array type', () => {
      const schema = {
        items: { default: [1, 2], rules: { type: 'array', itemType: 'number', minLength: 1 } },
      };
      expect(vu.validateSchema({ items: [3, 4, 5] }, schema, 'test').items).toEqual([3, 4, 5]);
      expect(vu.validateSchema({ items: 'not-array' }, schema, 'test').items).toEqual([1, 2]);
    });

    it('should handle nested object with schema recursively', () => {
      const schema = {
        logging: {
          rules: { type: 'object', schema: {
            enabled: { default: true, rules: { type: 'boolean' } },
            level: { default: 'info', rules: { type: 'string' } },
          }},
        },
      };
      const result = vu.validateSchema(
        { logging: { enabled: false, level: 'Debug' } },
        schema,
        'test'
      );
      expect(result.logging.enabled).toBe(false);
      expect(result.logging.level).toBe('debug');
    });

    it('should skip reserved keys (rules, description, schema)', () => {
      const schema = {
        rules: 'should be skipped',
        description: 'should be skipped',
        schema: 'should be skipped',
        speed: { default: 10, rules: { type: 'number' } },
      };
      const result = vu.validateSchema({}, schema, 'test');
      expect(result).not.toHaveProperty('rules');
      expect(result).not.toHaveProperty('description');
      expect(result).not.toHaveProperty('schema');
      expect(result.speed).toBe(10);
    });

    it('should use default for unknown validation type', () => {
      const schema = {
        weird: { default: 'fallback', rules: { type: 'unknownType' } },
      };
      const result = vu.validateSchema({ weird: 'value' }, schema, 'test');
      expect(result.weird).toBe('fallback');
    });

    it('should handle curve type', () => {
      const schema = {
        curve: {
          default: { line1: { x: [0, 1], y: [0, 1] } },
          rules: { type: 'curve' },
        },
      };
      const validCurve = { line1: { x: [1, 2], y: [10, 20] } };
      const result = vu.validateSchema({ curve: validCurve }, schema, 'test');
      expect(result.curve.line1.x).toEqual([1, 2]);
    });
  });

  // ── removeUnwantedKeys() ────────────────────────────────────────────
  describe('removeUnwantedKeys()', () => {
    it('should remove rules and description keys', () => {
      const input = {
        speed: { default: 10, rules: { type: 'number' }, description: 'Speed setting' },
      };
      const result = vu.removeUnwantedKeys(input);
      expect(result.speed).toBe(10);
    });

    it('should recurse into nested objects', () => {
      const input = {
        logging: {
          enabled: { default: true, rules: {} },
          level: { default: 'info', description: 'Log level' },
        },
      };
      const result = vu.removeUnwantedKeys(input);
      expect(result.logging.enabled).toBe(true);
      expect(result.logging.level).toBe('info');
    });

    it('should handle arrays', () => {
      const input = [
        { a: { default: 1, rules: {} } },
        { b: { default: 2, description: 'x' } },
      ];
      const result = vu.removeUnwantedKeys(input);
      expect(result[0].a).toBe(1);
      expect(result[1].b).toBe(2);
    });

    it('should return primitives as-is', () => {
      expect(vu.removeUnwantedKeys(42)).toBe(42);
      expect(vu.removeUnwantedKeys('hello')).toBe('hello');
      expect(vu.removeUnwantedKeys(null)).toBeNull();
    });
  });
});
