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

ROOT_DIR="${1:-/mnt/ssd/home/plx/clientflow_backend}"
SYSTEMD_DIR="/etc/systemd/system"

if [[ ! -d "$ROOT_DIR" ]]; then
  echo "Project directory not found: $ROOT_DIR" >&2
  exit 1
fi

cd "$ROOT_DIR"

sudo cp deploy/systemd/clientflow-outbox-jasmin.service "$SYSTEMD_DIR/"
sudo cp deploy/systemd/clientflow-outbox-jasmin.timer "$SYSTEMD_DIR/"

if [[ "${ENABLE_FISCAL_ENRICHMENT_TIMER:-false}" == "true" ]]; then
  sudo cp deploy/systemd/clientflow-fiscal-enrichment.service "$SYSTEMD_DIR/"
  sudo cp deploy/systemd/clientflow-fiscal-enrichment.timer "$SYSTEMD_DIR/"
fi

if [[ "${ENABLE_PACKLINK_TIMER:-false}" == "true" ]]; then
  sudo cp deploy/systemd/clientflow-outbox-packlink.service "$SYSTEMD_DIR/"
  sudo cp deploy/systemd/clientflow-outbox-packlink.timer "$SYSTEMD_DIR/"
fi

sudo systemctl daemon-reload
sudo systemctl enable --now clientflow-outbox-jasmin.timer

if [[ "${ENABLE_FISCAL_ENRICHMENT_TIMER:-false}" == "true" ]]; then
  sudo systemctl enable --now clientflow-fiscal-enrichment.timer
fi

if [[ "${ENABLE_PACKLINK_TIMER:-false}" == "true" ]]; then
  sudo systemctl enable --now clientflow-outbox-packlink.timer
fi

systemctl list-timers | grep clientflow || true
