# GitLab CI/CD Pipeline per VigilanzaTurni
# Deployment automatico su AlmaLinux 9

stages:
  - build
  - test
  - deploy

variables:
  NODE_VERSION: "20"
  APP_DIR: "/var/www/vigilanza-turni"

# Cache per velocizzare build
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - node_modules/
    - .npm/

# =================== BUILD STAGE ===================
build:
  stage: build
  image: node:20-alpine
  script:
    - echo "📦 Installazione dipendenze..."
    - npm ci
    - echo "🏗️  Build TypeScript..."
    - npm run build
    - echo "✅ Build completato"
  artifacts:
    paths:
      - dist/
      - node_modules/
    expire_in: 1 hour
  only:
    - main
    - develop

# =================== TEST STAGE ===================
test:
  stage: test
  image: node:20-alpine
  script:
    - echo "🧪 Esecuzione test..."
    - npm run lint || true
    - echo "✅ Test completati"
  only:
    - main
    - develop

# =================== DEPLOY PRODUCTION ===================
deploy_production:
  stage: deploy
  image: alpine:latest
  before_script:
    - apk add --no-cache openssh-client rsync
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -H $DEPLOY_HOST >> ~/.ssh/known_hosts
  script:
    - echo "🚀 Deployment su server produzione..."
    
    # Sync files to server
    - rsync -avz --delete --exclude='.git' --exclude='node_modules' --exclude='.env' ./ $DEPLOY_USER@$DEPLOY_HOST:$APP_DIR/
    
    # Execute deployment script on server
    - ssh $DEPLOY_USER@$DEPLOY_HOST "cd $APP_DIR && bash deploy/deploy.sh"
    
    - echo "✅ Deployment completato!"
    - echo "🌐 Applicazione disponibile su: https://$DEPLOY_DOMAIN"
  environment:
    name: production
    url: https://$DEPLOY_DOMAIN
  only:
    - main
  when: manual

# =================== DEPLOY STAGING (opzionale) ===================
deploy_staging:
  stage: deploy
  image: alpine:latest
  before_script:
    - apk add --no-cache openssh-client rsync
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -H $DEPLOY_HOST >> ~/.ssh/known_hosts
  script:
    - echo "🧪 Deployment su staging..."
    - rsync -avz --delete --exclude='.git' --exclude='node_modules' --exclude='.env' ./ $DEPLOY_USER@$DEPLOY_HOST:/var/www/vigilanza-turni-staging/
    - ssh $DEPLOY_USER@$DEPLOY_HOST "cd /var/www/vigilanza-turni-staging && bash deploy/deploy.sh"
    - echo "✅ Staging deployment completato!"
  environment:
    name: staging
    url: https://staging.$DEPLOY_DOMAIN
  only:
    - develop
  when: manual

# =================== ROLLBACK (emergenza) ===================
rollback:
  stage: deploy
  image: alpine:latest
  before_script:
    - apk add --no-cache openssh-client
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -H $DEPLOY_HOST >> ~/.ssh/known_hosts
  script:
    - echo "⏮️  Rollback alla versione precedente..."
    - ssh $DEPLOY_USER@$DEPLOY_HOST "cd $APP_DIR && git checkout HEAD~1 && bash deploy/deploy.sh"
    - echo "✅ Rollback completato"
  only:
    - main
  when: manual
