name: Deploy to Production

on:
  # Déclenchement manuel
  workflow_dispatch:
    inputs:
      environment:
        description: 'Environnement de déploiement'
        required: true
        default: 'deploy'
        type: choice
        options:
          - deploy
  # Déclenchement automatique
  pull_request:
    types: [closed]
    branches:
      - deploy

jobs:
  deploy:
    # S'exécute si c'est un déclenchement manuel OU si la PR a été mergée
    if: github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    # Configuration de PHP 8.4
    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.4'
        extensions: mbstring, xml, ctype, iconv, intl, pdo_mysql, dom, filter, gd, json
        tools: composer:v2.8.5

    # Création d'une base SQLite temporaire
    - name: Create temporary SQLite database
      run: |
        mkdir -p var
        touch var/app.db

    # Installation des dépendances Composer
    - name: Install Composer dependencies
      env:
        APP_ENV: prod
        DATABASE_URL: "sqlite:///%kernel.project_dir%/var/app.db"
      run: composer install --optimize-autoloader

    # Compilation des assets
    - name: Compile assets
      env:
        APP_ENV: prod
      run: |
        php bin/console tailwind:build --minify
        php bin/console asset-map:compile
        php bin/console assets:install public

    # Configuration de l'environnement
    - name: Create .env.local
      run: |
        echo "${{ secrets.ENV_FILE }}" > .env.local
      
    # Préparation des fichiers pour le déploiement
    - name: Prepare files for deployment
      run: |
        rm -rf .git/
        rm -rf tests/
        
    - name: Deploy to SFTP
      uses: SamKirkland/FTP-Deploy-Action@v4.3.4
      with:
        server: ${{ secrets.FTP_SERVER }}
        username: ${{ secrets.FTP_USERNAME }}
        password: ${{ secrets.FTP_PASSWORD }}
        local-dir: ./
        server-dir: ${{ secrets.FTP_SERVER_DIR }}/
        protocol: sftp
        port: 22
        timeout: 60000
        dangerous-clean-slate: false
        strict: false

    # Chargement des fixtures
    - name: Execute remote commands
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SSH_HOST }}
        username: ${{ secrets.SSH_USERNAME }}
        password: ${{ secrets.SSH_PASSWORD }}
        port: ${{ secrets.SSH_PORT }}
        script: |
          cd ${{ secrets.PROJECT_PATH }}
          php bin/console doctrine:fixtures:load --no-interaction

    # Notification de déploiement réussi
    - name: Deployment Success Notification
      if: success()
      uses: actions/github-script@v6
      with:
        script: |
          github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.name,
            body: '✅ Déploiement effectué avec succès!'
          })
