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

/**
 * AST node for measure definitions
 * Represents: measure userRole(user: User) { ... } PROVIDES string
 */
export class MeasureNode extends BaseNode {
  constructor(name, location = null) {
    super('Measure', location);
    this.name = name;
    this.parameters = [];
    this.returnType = null;
    this.body = null; // MeasureBodyNode
    this.provides = null; // Return type specification
  }

  /**
   * Add a parameter to the measure
   * @param {ParameterNode} parameter - Parameter to add
   */
  addParameter(parameter) {
    this.parameters.push(parameter);
    this.addChild(parameter);
  }

  /**
   * Set the body of the measure
   * @param {MeasureBodyNode} body - Measure body
   */
  setBody(body) {
    this.body = body;
    this.addChild(body);
  }

  /**
   * Set the return type for this measure
   * @param {string} returnType - Return type
   */
  setReturnType(returnType) {
    this.returnType = returnType;
    this.provides = returnType;
  }

  /**
   * Get the parameter names as an array
   * @returns {string[]} Array of parameter names
   */
  getParameterNames() {
    return this.parameters.map(param => param.name);
  }

  /**
   * Get the parameter types as an array
   * @returns {string[]} Array of parameter types
   */
  getParameterTypes() {
    return this.parameters.map(param => param.type);
  }

  /**
   * Find a parameter by name
   * @param {string} name - Parameter name to find
   * @returns {ParameterNode|null} Found parameter or null
   */
  getParameter(name) {
    return this.parameters.find(param => param.name === name) || null;
  }

  /**
   * Get the signature string for this measure
   * @returns {string} Measure signature
   */
  getSignature() {
    const paramStr = this.parameters.map(param => `${param.name}: ${param.type}`).join(', ');
    return `${this.name}(${paramStr})`;
  }

  /**
   * Check if this measure has a return type
   * @returns {boolean} True if has return type
   */
  hasReturnType() {
    return this.returnType !== null;
  }

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

    // Validate measure name
    if (!this.name || typeof this.name !== 'string') {
      errors.push(`Invalid measure name: ${this.name}`);
    }

    // Validate parameters
    this.parameters.forEach((param, index) => {
      const paramErrors = param.validate ? param.validate() : [];
      errors.push(...paramErrors.map(err => `Parameter ${index + 1}: ${err}`));
    });

    // Validate body
    if (this.body) {
      const bodyErrors = this.body.validate ? this.body.validate() : [];
      errors.push(...bodyErrors);
    }

    return errors;
  }

  toString() {
    const providesStr = this.returnType ? ` PROVIDES ${this.returnType}` : '';
    return `Measure(${this.getSignature()}${providesStr})`;
  }
}
