#!/usr/bin/env bash
# Synthesize a controller from a FRET synthesis config using ltlsynt (Spot).
#
# Usage: ./scripts/synthesize.sh specs/synthesis_config.json [output_dir]
#
# Reads the synthesis_config.json produced by parse_fret_json.py and
# runs ltlsynt to produce an AIGER circuit.

set -euo pipefail

CONFIG="${1:?Usage: $0 <synthesis_config.json> [output_dir]}"
OUTPUT_DIR="${2:-circuits}"

if ! command -v ltlsynt &>/dev/null; then
    echo "Error: ltlsynt not found. Install with: brew install spot"
    exit 1
fi

mkdir -p "$OUTPUT_DIR"

# Extract synthesis parameters from config
eval "$(python3 -c "
import json, sys, shlex

config = json.load(open(sys.argv[1]))
inputs = config['inputs']
outputs = config['outputs']
ltl = config.get('conjoined_ltl')
spec_name = config['spec_name']

if not ltl:
    print('echo \"Error: no conjoined LTL formula in config\"; exit 1')
    sys.exit(0)
if not outputs:
    print('echo \"Error: no output variables defined\"; exit 1')
    sys.exit(0)

print(f'SPEC_NAME={shlex.quote(spec_name)}')
print(f'INPUTS={shlex.quote(\",\".join(inputs))}')
print(f'OUTPUTS={shlex.quote(\",\".join(outputs))}')
print(f'LTL_FORMULA={shlex.quote(ltl)}')
" "$CONFIG")"

AAG_FILE="$OUTPUT_DIR/${SPEC_NAME}.aag"

echo "=== ltlsynt Reactive Synthesis ==="
echo "Spec:    $SPEC_NAME"
echo "Inputs:  $INPUTS"
echo "Outputs: $OUTPUTS"
echo "Formula: ${LTL_FORMULA:0:200}..."
echo ""
echo "Running ltlsynt..."

# Run ltlsynt
# -f: LTL formula
# --ins: uncontrollable (input/environment) propositions
# --outs: controllable (output/system) propositions
# --aiger: output as AIGER circuit
# --simplify=bwoa-sat: best simplification
SYNTH_OUTPUT=$(ltlsynt -f "$LTL_FORMULA" --ins="$INPUTS" --outs="$OUTPUTS" --aiger=both+ud+dc --simplify=bwoa-sat 2>&1) || true

if echo "$SYNTH_OUTPUT" | head -1 | grep -q "^REALIZABLE"; then
    echo ""
    echo "Result: REALIZABLE"
    # Strip the REALIZABLE line and write the AIGER circuit
    echo "$SYNTH_OUTPUT" | tail -n +2 > "$AAG_FILE"
    LINES=$(wc -l < "$AAG_FILE")
    echo "Circuit written to: $AAG_FILE ($LINES lines)"
elif echo "$SYNTH_OUTPUT" | head -1 | grep -q "^UNREALIZABLE"; then
    echo ""
    echo "Result: UNREALIZABLE"
    echo "The specification cannot be implemented as a controller."
    echo "$SYNTH_OUTPUT"
    exit 1
else
    echo ""
    echo "Error: unexpected ltlsynt output:"
    echo "$SYNTH_OUTPUT"
    exit 1
fi
