#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$ROOT_DIR"

if [ ! -f ".env" ]; then
  echo "Fel: .env saknas i $ROOT_DIR"
  exit 1
fi

if grep -q '^RECEIPT_TRACE_DECISIONS=' .env; then
  sed -i 's/^RECEIPT_TRACE_DECISIONS=.*/RECEIPT_TRACE_DECISIONS=1/' .env
else
  echo 'RECEIPT_TRACE_DECISIONS=1' >> .env
fi

echo "RECEIPT_TRACE_DECISIONS=1 satt i .env"

echo "Bygger och startar om recipe-api..."
docker compose -f compose.yml -f compose.flutter.yml up -d --build recipe-api

mkdir -p logs
LOG_FILE="logs/receipt_decisions_$(date +%Y%m%d_%H%M%S).log"
echo "Startar loggning till $LOG_FILE"
echo "Tryck Ctrl+C för att avsluta loggströmmen."

docker compose -f compose.yml -f compose.flutter.yml logs -f recipe-api | tee "$LOG_FILE"
