# Author: Ahmad Samir <a.atta@exp-sa.com>
name: Prevent Invalid Merges

on:
  pull_request:
    branches:
      - dev_odex-event
      - dev_odex25_accounting
      - dev_odex25_base
      - dev_odex25_dms
      - dev_odex25_fleet
      - dev_odex25_hr
      - dev_odex25_inventory
      - dev_odex25_maintenance
      - dev_odex25_mobile
      - dev_odex25_pos
      - dev_odex25_project
      - dev_odex25_purchase
      - dev_odex25_realstate
      - dev_odex25_sales
      - dev_odex25_survey
      - dev_odex25_transactions
      - dev_odex25_website
      - dev_openeducat_erp-14.0.1.0
      - dev_odex25_ensan
      - dev_odex25_helpdesk
      - dev_odex25_donation
      - dev_odex25_benefit
      - dev_odex25_takaful

      - preprod_odex-event
      - preprod_odex25_accounting
      - preprod_odex25_base
      - preprod_odex25_dms
      - preprod_odex25_fleet
      - preprod_odex25_hr
      - preprod_odex25_inventory
      - preprod_odex25_maintenance
      - preprod_odex25_mobile
      - preprod_odex25_pos
      - preprod_odex25_project
      - preprod_odex25_purchase
      - preprod_odex25_realstate
      - preprod_odex25_sales
      - preprod_odex25_survey
      - preprod_odex25_transactions
      - preprod_odex25_website
      - preprod_openeducat_erp-14.0.1.0
      - preprod_odex25_ensan
      - preprod_odex25_helpdesk
      - preprod_odex25_donation
      - preprod_odex25_benefit
      - preprod_odex25_takaful

      - master_odex-event 
      - master_odex25_accounting
      - master_odex25_base
      - master_odex25_dms
      - master_odex25_fleet
      - master_odex25_hr
      - master_odex25_inventory
      - master_odex25_maintenance
      - master_odex25_mobile
      - master_odex25_pos
      - master_odex25_project
      - master_odex25_purchase
      - master_odex25_realstate
      - master_odex25_sales
      - master_odex25_survey
      - master_odex25_transactions
      - master_odex25_website
      - master_openeducat_erp-14.0.1.0
      - master_odex25_ensan 
      - master_odex25_helpdesk
      - master_odex25_donation
      - master_odex25_benefit
      - master_odex25_takaful
      

jobs:
  validate-merge-source:
    runs-on: linting_odex25-standard-modules_runner
    steps:
      - name: Check source branch for merge
        run: |
          echo "Base branch: ${{ github.base_ref }}"
          echo "Head branch: ${{ github.head_ref }}"


          # ✅  Preprod Branches
          if [[ "${{ github.base_ref }}" == "preprod_odex-event" && "${{ github.head_ref }}" != "dev_odex-event" ]]; then
            echo "::error ::You can only merge to 'preprod_odex-event' from 'dev_odex-event' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_accounting" && "${{ github.head_ref }}" != "dev_odex25_accounting" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_accounting' from 'dev_odex25_accounting' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_base" && "${{ github.head_ref }}" != "dev_odex25_base" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_base' from 'dev_odex25_base' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_dms" && "${{ github.head_ref }}" != "dev_odex25_dms" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_dms' from 'dev_odex25_dms' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_fleet" && "${{ github.head_ref }}" != "dev_odex25_fleet" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_fleet' from 'dev_odex25_fleet' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_hr" && "${{ github.head_ref }}" != "dev_odex25_hr" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_hr' from 'dev_odex25_hr' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_inventory" && "${{ github.head_ref }}" != "dev_odex25_inventory" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_inventory' from 'dev_odex25_inventory' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_maintenance" && "${{ github.head_ref }}" != "dev_odex25_maintenance" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_maintenance' from 'dev_odex25_maintenance' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_mobile" && "${{ github.head_ref }}" != "dev_odex25_mobile" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_mobile' from 'dev_odex25_mobile' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_pos" && "${{ github.head_ref }}" != "dev_odex25_pos" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_pos' from 'dev_odex25_pos' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_project" && "${{ github.head_ref }}" != "dev_odex25_project" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_project' from 'dev_odex25_project' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_purchase" && "${{ github.head_ref }}" != "dev_odex25_purchase" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_purchase' from 'dev_odex25_purchase' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_realstate" && "${{ github.head_ref }}" != "dev_odex25_realstate" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_realstate' from 'dev_odex25_realstate' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_sales" && "${{ github.head_ref }}" != "dev_odex25_sales" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_sales' from 'dev_odex25_sales' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_survey" && "${{ github.head_ref }}" != "dev_odex25_survey" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_survey' from 'dev_odex25_survey' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_transactions" && "${{ github.head_ref }}" != "dev_odex25_transactions" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_transactions' from 'dev_odex25_transactions' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_website" && "${{ github.head_ref }}" != "dev_odex25_website" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_website' from 'dev_odex25_website' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_openeducat_erp-14.0.1.0" && "${{ github.head_ref }}" != "dev_openeducat_erp-14.0.1.0" ]]; then
            echo "::error ::You can only merge to 'preprod_openeducat_erp-14.0.1.0' from 'dev_openeducat_erp-14.0.1.0' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_ensan" && "${{ github.head_ref }}" != "dev_odex25_ensan" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_ensan' from 'dev_odex25_ensan' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_helpdesk" && "${{ github.head_ref }}" != "dev_odex25_helpdesk" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_helpdesk' from 'dev_odex25_helpdesk' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_donation" && "${{ github.head_ref }}" != "dev_odex25_donation" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_donation' from 'dev_odex25_donation' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_benefit" && "${{ github.head_ref }}" != "dev_odex25_benefit" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_donation' from 'dev_odex25_donation' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "preprod_odex25_takaful" && "${{ github.head_ref }}" != "dev_odex25_takaful" ]]; then
            echo "::error ::You can only merge to 'preprod_odex25_donation' from 'dev_odex25_donation' branch!"
            exit 1
          fi
          

          # ✅  Master Branches
          if [[ "${{ github.base_ref }}" == "master_odex-event" && "${{ github.head_ref }}" != "preprod_odex-event" ]]; then
            echo "::error ::You can only merge to 'master_odex-event' from 'preprod_odex-event' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_accounting" && "${{ github.head_ref }}" != "preprod_odex25_accounting" ]]; then
            echo "::error ::You can only merge to 'master_odex25_accounting' from 'preprod_odex25_accounting' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_base" && "${{ github.head_ref }}" != "preprod_odex25_base" ]]; then
            echo "::error ::You can only merge to 'master_odex25_base' from 'preprod_odex25_base' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_dms" && "${{ github.head_ref }}" != "preprod_odex25_dms" ]]; then
            echo "::error ::You can only merge to 'master_odex25_dms' from 'preprod_odex25_dms' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_fleet" && "${{ github.head_ref }}" != "preprod_odex25_fleet" ]]; then
            echo "::error ::You can only merge to 'master_odex25_fleet' from 'preprod_odex25_fleet' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_hr" && "${{ github.head_ref }}" != "preprod_odex25_hr" ]]; then
            echo "::error ::You can only merge to 'master_odex25_hr' from 'preprod_odex25_hr' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_inventory" && "${{ github.head_ref }}" != "preprod_odex25_inventory" ]]; then
            echo "::error ::You can only merge to 'master_odex25_inventory' from 'preprod_odex25_inventory' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_maintenance" && "${{ github.head_ref }}" != "preprod_odex25_maintenance" ]]; then
            echo "::error ::You can only merge to 'master_odex25_maintenance' from 'preprod_odex25_maintenance' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_mobile" && "${{ github.head_ref }}" != "preprod_odex25_mobile" ]]; then
            echo "::error ::You can only merge to 'master_odex25_mobile' from 'preprod_odex25_mobile' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_pos" && "${{ github.head_ref }}" != "preprod_odex25_pos" ]]; then
            echo "::error ::You can only merge to 'master_odex25_pos' from 'preprod_odex25_pos' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_project" && "${{ github.head_ref }}" != "preprod_odex25_project" ]]; then
            echo "::error ::You can only merge to 'master_odex25_project' from 'preprod_odex25_project' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_purchase" && "${{ github.head_ref }}" != "preprod_odex25_purchase" ]]; then
            echo "::error ::You can only merge to 'master_odex25_purchase' from 'preprod_odex25_purchase' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_realstate" && "${{ github.head_ref }}" != "preprod_odex25_realstate" ]]; then
            echo "::error ::You can only merge to 'master_odex25_realstate' from 'preprod_odex25_realstate' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_sales" && "${{ github.head_ref }}" != "preprod_odex25_sales" ]]; then
            echo "::error ::You can only merge to 'master_odex25_sales' from 'preprod_odex25_sales' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_survey" && "${{ github.head_ref }}" != "preprod_odex25_survey" ]]; then
            echo "::error ::You can only merge to 'master_odex25_survey' from 'preprod_odex25_survey' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_transactions" && "${{ github.head_ref }}" != "preprod_odex25_transactions" ]]; then
            echo "::error ::You can only merge to 'master_odex25_transactions' from 'preprod_odex25_transactions' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_website" && "${{ github.head_ref }}" != "preprod_odex25_website" ]]; then
            echo "::error ::You can only merge to 'master_odex25_website' from 'preprod_odex25_website' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_openeducat_erp-14.0.1.0" && "${{ github.head_ref }}" != "preprod_openeducat_erp-14.0.1.0" ]]; then
            echo "::error ::You can only merge to 'master_openeducat_erp-14.0.1.0' from 'preprod_openeducat_erp-14.0.1.0' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_ensan" && "${{ github.head_ref }}" != "preprod_odex25_ensan" ]]; then
            echo "::error ::You can only merge to 'master_odex25_ensan' from 'preprod_odex25_ensan' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_helpdesk" && "${{ github.head_ref }}" != "preprod_odex25_helpdesk" ]]; then
            echo "::error ::You can only merge to 'master_odex25_helpdesk' from 'preprod_odex25_helpdesk' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_donation" && "${{ github.head_ref }}" != "preprod_odex25_donation" ]]; then
            echo "::error ::You can only merge to 'master_odex25_donation' from 'preprod_odex25_donation' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_benefit" && "${{ github.head_ref }}" != "preprod_odex25_benefit" ]]; then
            echo "::error ::You can only merge to 'master_odex25_donation' from 'preprod_odex25_donation' branch!"
            exit 1
          fi

          if [[ "${{ github.base_ref }}" == "master_odex25_takaful" && "${{ github.head_ref }}" != "preprod_odex25_takaful" ]]; then
            echo "::error ::You can only merge to 'master_odex25_donation' from 'preprod_odex25_donation' branch!"
            exit 1
          fi
          

          


          # ❌ Block ALL merges to 'master'
          if [[ "${{ github.base_ref }}" == "master" ]]; then
            echo "::error ::Merging to 'master' branch is not allowed!"
            exit 1
          fi

          echo "Merge validation passed."
