#!/usr/bin/env bash
# Deploy integrazione YouTube su produzione (sync + env + migrate + rebuild).
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
ENV_FILE="${ENV_FILE:-$ROOT/infra/.env}"
SERVER="${DEPLOY_SERVER:-eminux@192.168.1.146}"
REMOTE_DIR="${DEPLOY_PATH:-/opt/matchlivetv}"
YOUTUBE_REDIRECT_URI="${YOUTUBE_REDIRECT_URI:-https://www.matchlivetv.it/api/v1/youtube/callback}"

if [[ ! -f "$ENV_FILE" ]]; then
  echo "File non trovato: $ENV_FILE" >&2
  exit 1
fi

read_env() {
  local key="$1"
  grep -E "^${key}=" "$ENV_FILE" | head -1 | cut -d= -f2- | tr -d '\r'
}

YOUTUBE_CLIENT_ID="$(read_env YOUTUBE_CLIENT_ID)"
YOUTUBE_CLIENT_SECRET="$(read_env YOUTUBE_CLIENT_SECRET)"
YOUTUBE_PLATFORM_REFRESH_TOKEN="$(read_env YOUTUBE_PLATFORM_REFRESH_TOKEN)"

echo "Sync codice → $SERVER:$REMOTE_DIR ..."
bash "$ROOT/scripts/deploy/sync_to_server.sh"

echo "Aggiorno variabili YouTube in $REMOTE_DIR/infra/.env ..."
if [[ -n "$YOUTUBE_CLIENT_ID" && -n "$YOUTUBE_CLIENT_SECRET" ]]; then
  ssh "$SERVER" "bash -s" <<REMOTE
set -euo pipefail
ENV="$REMOTE_DIR/infra/.env"
touch "\$ENV"
upsert() {
  local key="\$1" val="\$2"
  if grep -q "^\${key}=" "\$ENV" 2>/dev/null; then
    sed -i "s|^\${key}=.*|\${key}=\${val}|" "\$ENV"
  else
    echo "\${key}=\${val}" >> "\$ENV"
  fi
}
upsert YOUTUBE_CLIENT_ID '${YOUTUBE_CLIENT_ID}'
upsert YOUTUBE_CLIENT_SECRET '${YOUTUBE_CLIENT_SECRET}'
REMOTE
else
  echo "  (locale senza YOUTUBE_* — uso valori già presenti sul server)"
fi

ssh "$SERVER" "bash -s" <<REMOTE
set -euo pipefail
ENV="$REMOTE_DIR/infra/.env"
touch "\$ENV"
if grep -q '^YOUTUBE_REDIRECT_URI=' "\$ENV" 2>/dev/null; then
  sed -i 's|^YOUTUBE_REDIRECT_URI=.*|YOUTUBE_REDIRECT_URI=${YOUTUBE_REDIRECT_URI}|' "\$ENV"
else
  echo 'YOUTUBE_REDIRECT_URI=${YOUTUBE_REDIRECT_URI}' >> "\$ENV"
fi
REMOTE

if [[ -n "$YOUTUBE_PLATFORM_REFRESH_TOKEN" ]]; then
  ssh "$SERVER" "grep -q '^YOUTUBE_PLATFORM_REFRESH_TOKEN=' $REMOTE_DIR/infra/.env && sed -i 's|^YOUTUBE_PLATFORM_REFRESH_TOKEN=.*|YOUTUBE_PLATFORM_REFRESH_TOKEN=${YOUTUBE_PLATFORM_REFRESH_TOKEN}|' $REMOTE_DIR/infra/.env || echo 'YOUTUBE_PLATFORM_REFRESH_TOKEN=${YOUTUBE_PLATFORM_REFRESH_TOKEN}' >> $REMOTE_DIR/infra/.env"
fi

echo "Rebuild stack + migrate..."
ssh "$SERVER" "cd $REMOTE_DIR/infra && docker compose -f docker-compose.prod.yml --env-file .env up -d --build rails sidekiq"
sleep 25
ssh "$SERVER" "cd $REMOTE_DIR/infra && docker compose -f docker-compose.prod.yml exec -T rails bundle exec rails db:migrate"
ssh "$SERVER" "cd $REMOTE_DIR/infra && docker compose -f docker-compose.prod.yml exec -T rails bundle exec rails db:seed" || true

echo "Verifica route admin YouTube..."
ssh "$SERVER" "cd $REMOTE_DIR/infra && docker compose -f docker-compose.prod.yml exec -T rails bundle exec rails routes -g youtube_platform | head -5"

echo ""
echo "OK. Prossimi passi manuali:"
echo "  1. Admin: https://www.matchlivetv.it/admin/login"
echo "  2. Token canale Light: https://www.matchlivetv.it/admin/youtube/platform"
if [[ -z "$YOUTUBE_PLATFORM_REFRESH_TOKEN" ]]; then
  echo "     (poi aggiungi YOUTUBE_PLATFORM_REFRESH_TOKEN al .env server e riavvia rails)"
fi
echo "  3. Full: Dettagli squadra → Collega canale YouTube"
