package io.github.jjsimas.chart2d;

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

public record CartesianChartModel(
  String title,
  String categoryAxisTitle,
  String valueAxisTitle,
  List<String> categoryLabels,
  List<CartesianSeries> series,
  List<WarningBand> warningBands,
  CartesianChartOptions options,
  ChartTheme theme
) {

  public CartesianChartModel {
    title = Objects.requireNonNullElse(title, "");
    categoryAxisTitle = Objects.requireNonNullElse(categoryAxisTitle, "");
    valueAxisTitle = Objects.requireNonNullElse(valueAxisTitle, "");
    categoryLabels = List.copyOf(Objects.requireNonNull(categoryLabels, "categoryLabels"));
    series = List.copyOf(Objects.requireNonNull(series, "series"));
    warningBands = List.copyOf(Objects.requireNonNullElse(warningBands, List.of()));
    options = Objects.requireNonNullElse(options, CartesianChartOptions.defaults());
    theme = Objects.requireNonNullElse(theme, ChartTheme.defaults());
    if (series.isEmpty()) {
      throw new IllegalArgumentException("series must not be empty");
    }
    int maxValues = series.stream().mapToInt(item -> item.values().size()).max().orElse(0);
    if (categoryLabels.isEmpty() && maxValues > 0) {
      throw new IllegalArgumentException("categoryLabels must not be empty when series contain values");
    }
  }
}
