# MotoVaultPro Maintenance Migration Workflow
# Manual trigger for breaking database migrations requiring downtime

name: Maintenance Migration
run-name: Maintenance Migration

on:
  workflow_dispatch:
    inputs:
      create_backup:
        description: 'Create database backup before migration'
        required: true
        default: 'yes'
        type: choice
        options:
          - 'yes'
          - 'no'

env:
  DEPLOY_PATH: /opt/motovaultpro

jobs:
  maintenance-migration:
    name: Run Maintenance Migration
    runs-on: prod
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Send maintenance start notification
        run: |
          cd "$DEPLOY_PATH"
          chmod +x scripts/ci/notify.sh
          ./scripts/ci/notify.sh maintenance_start "Starting maintenance window for database migration"
        env:
          DEPLOY_NOTIFY_EMAIL: ${{ vars.DEPLOY_NOTIFY_EMAIL }}
          RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }}

      - name: Run maintenance migration
        run: |
          cd "$DEPLOY_PATH"
          chmod +x scripts/ci/maintenance-migrate.sh
          if [ "${{ inputs.create_backup }}" = "yes" ]; then
            ./scripts/ci/maintenance-migrate.sh backup
          else
            ./scripts/ci/maintenance-migrate.sh
          fi

      - name: Send maintenance complete notification
        if: success()
        run: |
          cd "$DEPLOY_PATH"
          chmod +x scripts/ci/notify.sh
          ./scripts/ci/notify.sh maintenance_end "Maintenance window complete. Database migration successful."
        env:
          DEPLOY_NOTIFY_EMAIL: ${{ vars.DEPLOY_NOTIFY_EMAIL }}
          RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }}

      - name: Send maintenance failure notification
        if: failure()
        run: |
          cd "$DEPLOY_PATH"
          chmod +x scripts/ci/notify.sh
          ./scripts/ci/notify.sh failure "Maintenance migration FAILED. Manual intervention required."
        env:
          DEPLOY_NOTIFY_EMAIL: ${{ vars.DEPLOY_NOTIFY_EMAIL }}
          RESEND_API_KEY: ${{ secrets.RESEND_API_KEY }}
