#!/bin/bash

# 1. Automatically find the Sagittarius container ID
CONTAINER_ID=$(docker ps | grep "sagittarius" | awk '{print $1}' | head -n 1)

if [ -z "$CONTAINER_ID" ]; then
    echo "❌ Error: Could not find a running container matching 'sagittarius'."
    exit 1
fi

# 2. Prompt you for the flow ID
read -p "Enter Flow ID to validate: " FLOW_ID

if [ -z "$FLOW_ID" ]; then
    echo "❌ Error: Flow ID cannot be empty."
    exit 1
fi

echo "⏳ Running Triangulum validation for Flow #$FLOW_ID..."
echo "--------------------------------------------------"

# 3. Feed the entire Ruby block into the Rails console
docker exec -i "$CONTAINER_ID" bin/rails c <<EOF
flow = Flow.find($FLOW_ID)
function_definitions = FunctionDefinition.by_node_function(flow.node_functions).preload(:runtime_function_definition)
data_types = DataType.where(runtime: flow.project.primary_runtime)

Triangulum::Validation.new(
  flow.to_grpc,
  function_definitions.map(&:to_grpc),
  data_types.map(&:to_grpc)
).validate
EOF
