/*
 * RiskScape™ Copyright New Zealand Institute for Earth Science Limited
 * (Earth Sciences New Zealand) is distributed for research purposes only
 * under the terms of AGPLv3.
 *
 * RiskScape™ Copyright 2026 New Zealand Institute for Earth Science
 * Limited (Earth Sciences New Zealand). All rights reserved. Source code
 * available under the AGPLv3.
 * 
 * This program is free software: you can redistribute it and/or modify it under
 *  the terms of the GNU Affero General Public License as published by the Free
 *  Software Foundation, either version 3 of the License, or (at your option) any
 *  later version.
 * 
 * This program is distributed for RESEARCH PURPOSES ONLY, in the hope that it will
 * be useful for research and education initiatives.
 * 
 * If you are not a researcher, or you are a researcher who wishes to use this
 * program on terms other than AGPLv3 (including those who wish to restrict the
 * distribution of any source code created using this program), please contact:
 * https://riskscape.org.nz
 * 
 * This program is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.  You should have received a copy
 * of the GNU Affero General Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * By way of summary only, under the AGPLv3:
 *     • Permissions of this strongest copyleft license are conditioned
 *       on making available complete source code of licensed works and
 *       modifications, which include larger works using a licensed work,
 *       under the same license.
 *     • Copyright and license notices must be preserved.
 *     • Contributors provide an express grant of patent rights.
 *     • When a modified version is used to provide a service over a
 *       network, the complete source code of the modified version must be made
 *       available.
 */
package nz.org.riskscape.rl.ast;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import nz.org.riskscape.dsl.Token;
import nz.org.riskscape.engine.util.Pair;

/**
 * Represents a chain of input that is being sent as input to a step as a parameter, e.g.
 * `join(rhs: <- input(...) -> ...)`
 */
@RequiredArgsConstructor
public class InputChain extends BaseAST implements PipelineAST {

  /**
   * The `<-` token
   */
  @Getter
  private final Token inputToken;

  @Getter
  private final StepChain chain;

  @Override
  public Optional<Pair<Token, Token>> getBoundary() {
    return chain.getLast().getBoundary().map(boundary -> Pair.of(inputToken, boundary.getRight()));
  }

  /**
   * @return a list of all the references to steps in the pipeline made from this input chain, e.g will return the foo
   * step reference declared within the rhs of the join in `input() as foo ... join(rhs: <- foo -> group())`
   */
  public List<StepReference> getStepReferences() {
    return chain.getSteps().stream()
        .map(step -> step.isA(StepReference.class).orElse(null))
        .filter(Objects::nonNull)
        .toList();
  }

  @Override
  public <T, R> R accept(PipelineVisitor<T, R> visitor, T data) {
    return visitor.visit(this, data);
  }

  @Override
  protected void appendSource(StringBuilder appendTo) {
    appendTo.append("<- ");
    chain.appendSource(appendTo);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof InputChain rhs) {
      return rhs.chain.equals(this.chain);
    }

    return false;
  }

  @Override
  public int hashCode() {
    return chain.hashCode();
  }

  /**
   * @return a step reference if that is the only item in this chain, e.g. the whole thing is just a reference
   * to another  step in the pipeline.
   */
  public Optional<StepReference> getAsStepReference() {
    if (chain.getSteps().size() == 1) {
      return chain.getFirst().isA(StepReference.class);
    }

    return Optional.empty();
  }
}
