name: 🔁 Branch Promotions

on:
  schedule:
    - cron: "0 6 * * 1,4" # Lundi et Jeudi à 6h UTC (design → develop)
    - cron: "0 6 * * 1" # Lundi à 6h UTC (develop → main)
  workflow_dispatch:
    inputs:
      promotion:
        description: "Which promotion to run"
        required: true
        type: choice
        options:
          - design-to-develop
          - develop-to-main
          - both

permissions:
  contents: read
  pull-requests: write

concurrency:
  group: branch-promotions
  cancel-in-progress: false

jobs:
  design-to-develop:
    name: Open design → develop
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'schedule') ||
      (github.event_name == 'workflow_dispatch' && (github.event.inputs.promotion == 'design-to-develop' || github.event.inputs.promotion == 'both'))
    steps:
      - name: ⬇️ Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: 🔁 Open promotion PR
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail

          git fetch origin develop design

          if git merge-base --is-ancestor origin/design origin/develop; then
            echo "No promotion needed: develop already contains design."
            exit 0
          fi

          existing_pr="$(gh pr list \
            --state open \
            --base develop \
            --head "$GITHUB_REPOSITORY_OWNER:design" \
            --json number \
            --jq '.[0].number // empty')"

          if [ -n "$existing_pr" ]; then
            echo "Promotion PR already open: #$existing_pr."
            exit 0
          fi

          gh pr create \
            --base develop \
            --head design \
            --title "chore: merge design into develop" \
            --body "Automated promotion PR from \`design\` to \`develop\`."

  develop-to-main:
    name: Open develop → main
    runs-on: ubuntu-latest
    if: |
      (github.event_name == 'schedule' && github.event.schedule == '0 6 * * 1') ||
      (github.event_name == 'workflow_dispatch' && (github.event.inputs.promotion == 'develop-to-main' || github.event.inputs.promotion == 'both'))
    steps:
      - name: ⬇️ Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: 🔁 Open promotion PR
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail

          git fetch origin main develop

          if git merge-base --is-ancestor origin/develop origin/main; then
            echo "No promotion needed: main already contains develop."
            exit 0
          fi

          existing_pr="$(gh pr list \
            --state open \
            --base main \
            --head "$GITHUB_REPOSITORY_OWNER:develop" \
            --json number \
            --jq '.[0].number // empty')"

          if [ -n "$existing_pr" ]; then
            echo "Promotion PR already open: #$existing_pr."
            exit 0
          fi

          gh pr create \
            --base main \
            --head develop \
            --title "chore: merge develop into main" \
            --body "Automated weekly promotion PR from \`develop\` to \`main\`."
