#!/usr/bin/env bash

set -e


YAML_DIR=metadata_service/models/src
OUTPUT_DIR=metadata_service/models/generated

# Ensure the yaml directory exists
if [ ! -d "$YAML_DIR" ]; then
  echo "The yaml directory does not exist: $YAML_DIR"
  exit 1
fi


rm -rf "$OUTPUT_DIR"/*.py
mkdir -p "$OUTPUT_DIR"

echo "# generated by generate-metadata-models" > "$OUTPUT_DIR"/__init__.py

for f in "$YAML_DIR"/*.yaml; do
  filename_wo_ext=$(basename "$f" | cut -d . -f 1)
  echo "from .$filename_wo_ext import *" >> "$OUTPUT_DIR"/__init__.py

  datamodel-codegen \
    --input "$YAML_DIR/$filename_wo_ext.yaml" \
    --output "$OUTPUT_DIR/$filename_wo_ext.py" \
    --use-title-as-name \
    --use-double-quotes \
    --enum-field-as-literal all \
    --disable-timestamp
done

echo ""
echo "Generating bundled JSON schema..."
node bin/bundle-schemas.js
