name: Create Branch File

on:
  create:
    branches:
      - 'committee/*'

jobs:
  create-file:
    runs-on: ubuntu-latest
    steps:
      - name: Check out code
        uses: actions/checkout@v3
        with: 
          ref: committee/test

      - name: Generate Committee File
        run: |
          branch_name=${{ github.ref }}
          branch_name=${branch_name#refs/heads/committee/}
          sed "s/class example/class $branch_name/g" src/Committees/example.py > "src/Committees/${branch_name}.py"
        shell: bash

      - name: Commit and Push Changes
        run: |
          branch_name=${{ github.ref }}
          branch_name=${branch_name#refs/heads/committee/}
          git config --global user.email "actions@github.com"
          git config --global user.name "GitHub Actions"
          git add "./src/committee/${branch_name}.py"
          git commit -m "Add ${branch_name}.py"
          git push origin $branch_name
        shell: bash

      - name: Create Committee Team
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          branch_name=${{ github.ref }}
          branch_name=${branch_name#refs/heads/committee/}
          curl -X POST \
            -H "Authorization: Bearer $GITHUB_TOKEN" \
            -d '{"name":"'$branch_name'","privacy":"closed"}' \
            https://api.github.com/orgs/Telegram-BX-Bot/teams
