/*
 * Copyright © 2021 DataSQRL (contact@datasqrl.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.datasqrl.plan.table;

import lombok.Value;

@Value
public class TableStatistic {

  private static final double DEFAULT_ROW_COUNT = 1e15;

  public static final TableStatistic UNKNOWN = new TableStatistic(Double.NaN, true);
  public static final double DEFAULT_NESTED_MULTIPLIER = 2.0;

  private final double rowCount;
  private final boolean isEstimated;

  public static TableStatistic fromEstimate(double rowCount) {
    return new TableStatistic(rowCount, true);
  }

  public static TableStatistic fromHint(double rowCount) {
    return new TableStatistic(rowCount, false);
  }

  @Override
  public String toString() {
    return "Stats=" + rowCount;
  }

  public boolean isUnknown() {
    return Double.isNaN(rowCount);
  }

  public TableStatistic nested() {
    return nested(DEFAULT_NESTED_MULTIPLIER);
  }

  public TableStatistic nested(double multiplier) {
    if (isUnknown()) {
      return UNKNOWN;
    }
    return new TableStatistic(rowCount * multiplier, isEstimated);
  }

  public double getRowCount() {
    if (isUnknown()) {
      return DEFAULT_ROW_COUNT;
    } else {
      return rowCount;
    }
  }
}
