image: php:8.2-cli

pipelines:
  default:
    - step:
        name: Install Dependencies and Test
        caches:
          - composer
        services:
          - mysql
        script:
          - apt-get update && apt-get install -y openssh-client unzip libzip-dev
          - docker-php-ext-install zip pdo_mysql
          
          # Installation de Composer
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install --prefer-dist --no-progress --no-suggest
          
          # Définition de l'hôte MySQL et attente du démarrage
          - echo "Attente du démarrage de MySQL..."
          - for i in {1..30}; do
              if mysqladmin ping -h"mysql" --silent; then
                echo "MySQL est prêt !"
                break
              fi
              echo "En attente de MySQL..."
              sleep 2
            done

          # Génération de la clé Laravel et migration
          - cp .env.example .env
          - echo "APP_ENV=testing" >> .env
          - echo "APP_KEY=test" >> .env
          - echo "DB_CONNECTION=mysql" >> .env
          - echo "DB_HOST=127.0.0.1" >> .env
          - echo "DB_PORT=3306" >> .env
          - echo "DB_DATABASE=iris_bdd" >> .env
          - echo "DB_USERNAME=iris_user" >> .env
          - echo "DB_PASSWORD=iris@2025" >> .env

          # Génération de la clé d'application
          - php artisan key:generate

          # Exécution des migrations
          - php artisan migrate --force

          # Exécution des tests
          - php artisan test

  branches:
    master:
      - step:
          name: Deploy to Production
          script:
            # Configuration SSH sécurisée
            - apt-get update && apt-get install -y openssh-client
            - mkdir -p ~/.ssh
            - echo "$SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
            - chmod 600 ~/.ssh/id_rsa
            - ssh-keyscan -H $PRODUCTION_SERVER_IP >> ~/.ssh/known_hosts

            # Vérification et clonage du dépôt si nécessaire
            - ssh -o StrictHostKeyChecking=no $SSH_USER@$PRODUCTION_SERVER_IP "if [ ! -d \"public_html/iris_auth/.git\" ]; then echo \"Le dépôt n'existe pas, clonage...\"; git clone \"$REPO_URL\" \"public_html/iris_auth\"; fi"

            # Mise à jour du dépôt et déploiement
            - ssh -o StrictHostKeyChecking=no $SSH_USER@$PRODUCTION_SERVER_IP "cd \"public_html/iris_auth\" && git fetch origin && git reset --hard origin/master"

            # Installation des dépendances
            - ssh -o StrictHostKeyChecking=no $SSH_USER@$PRODUCTION_SERVER_IP "cd \"public_html/iris_auth\" && composer install --no-dev --prefer-dist --optimize-autoloader"

            # Exécution des migrations et optimisation
            - ssh -o StrictHostKeyChecking=no $SSH_USER@$PRODUCTION_SERVER_IP "cd \"public_html/iris_auth\" && php artisan migrate --force && php artisan config:cache && php artisan route:cache && php artisan view:cache && php artisan optimize && php artisan queue:restart"

definitions:
  services:
    mysql:
      image: mysql:8.0
      environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: iris_bdd
        MYSQL_USER: iris_user
        MYSQL_PASSWORD: iris@2025
