import { BaseNode } from './BaseNode.js';

/**
 * AST node for field behaviors (decay, blur, ttl)
 * Represents: BEHAVES { decaying down hourly }
 */
export class BehaviorNode extends BaseNode {
  constructor(type, location = null) {
    super('Behavior', location);
    this.type = type; // 'decay', 'blur', 'ttl'
    this.parameters = new Map();
  }

  /**
   * Set a parameter for this behavior
   * @param {string} name - Parameter name
   * @param {*} value - Parameter value
   */
  setParameter(name, value) {
    this.parameters.set(name, value);
  }

  /**
   * Get a parameter value
   * @param {string} name - Parameter name
   * @returns {*} Parameter value or null
   */
  getParameter(name) {
    return this.parameters.get(name) || null;
  }

  /**
   * Check if this is a decay behavior
   * @returns {boolean} True if decay behavior
   */
  isDecay() {
    return this.type === 'decay';
  }

  /**
   * Check if this is a blur behavior
   * @returns {boolean} True if blur behavior
   */
  isBlur() {
    return this.type === 'blur';
  }

  /**
   * Check if this is a TTL behavior
   * @returns {boolean} True if TTL behavior
   */
  isTTL() {
    return this.type === 'ttl';
  }

  /**
   * For decay behaviors, get the direction
   * @returns {string|null} Decay direction or null
   */
  getDecayDirection() {
    return this.getParameter('direction');
  }

  /**
   * For decay behaviors, get the period
   * @returns {string|null} Decay period or null
   */
  getDecayPeriod() {
    return this.getParameter('period');
  }

  /**
   * For blur behaviors, get the mode
   * @returns {string|null} Blur mode or null
   */
  getBlurMode() {
    return this.getParameter('mode');
  }

  /**
   * For blur behaviors, get the confidence level
   * @returns {string|null} Confidence level or null
   */
  getBlurConfidence() {
    return this.getParameter('confidence');
  }

  /**
   * For TTL behaviors, get the duration
   * @returns {string|null} TTL duration or null
   */
  getTTLDuration() {
    return this.getParameter('duration');
  }

  /**
   * Validate the behavior
   * @returns {string[]} Array of error messages
   */
  validate() {
    const errors = [];

    // Validate behavior type
    if (!['decay', 'blur', 'ttl'].includes(this.type)) {
      errors.push(`Invalid behavior type: ${this.type}`);
    }

    // Validate decay behavior parameters
    if (this.isDecay()) {
      const direction = this.getDecayDirection();
      if (!direction || !['up', 'down', 'neutral', 'stable'].includes(direction)) {
        errors.push(`Invalid decay direction: ${direction}`);
      }

      const period = this.getDecayPeriod();
      if (!period || !['hourly', 'daily', 'weekly', 'monthly'].includes(period)) {
        errors.push(`Invalid decay period: ${period}`);
      }
    }

    // Validate blur behavior parameters
    if (this.isBlur()) {
      const mode = this.getBlurMode();
      if (!mode || !['fixed', 'adaptive', 'confidence'].includes(mode)) {
        errors.push(`Invalid blur mode: ${mode}`);
      }

      const confidence = this.getBlurConfidence();
      if (confidence && !['confidence_90', 'confidence_95', 'confidence_99'].includes(confidence)) {
        errors.push(`Invalid blur confidence: ${confidence}`);
      }
    }

    // Validate TTL behavior parameters
    if (this.isTTL()) {
      const duration = this.getTTLDuration();
      if (!duration || !/^\d+[hd]$/.test(duration)) {
        errors.push(`Invalid TTL duration: ${duration}`);
      }
    }

    return errors;
  }

  toString() {
    const params = Array.from(this.parameters.entries())
      .map(([key, value]) => `${key}: ${value}`)
      .join(', ');
    return `Behavior(${this.type}, ${params})`;
  }
}
