name: Handle VM Deployment Issue
on:
  issues:
    types: [opened, reopened]

jobs:
  process-issue:
    if: contains(github.event.issue.labels.*.name, 'opentofu')
    runs-on: docker
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Extract Issue Details
        id: extract
        run: |
          echo "Extracting issue details..."
          ISSUE_BODY="${{ github.event.issue.body }}"
          VM_NAME=$(echo "$ISSUE_BODY" | grep -A 2 'VM Name' | tail -n 1 | xargs)
          VM_ID=$(echo "$ISSUE_BODY" | grep -A 2 'VM UID' | tail -n 1 | xargs)
          RAM=$(echo "$ISSUE_BODY" | grep -A 2 'RAM' | tail -n 1 | xargs)
          CPUS=$(echo "$ISSUE_BODY" | grep -A 2 'CPU Cores' | tail -n 1 | xargs)
          SCPU_TYPE=$(echo "$ISSUE_BODY" | grep -A 2 'CPU Type' | tail -n 1 | xargs)
          NODE_NAME=$(echo "$ISSUE_BODY" | grep -A 2 'Node Name' | tail -n 1 | xargs)
          VLAN_ID=$(echo "$ISSUE_BODY" | grep -A 2 'VLAN ID' | tail -n 1 | xargs)
          DATASTORE=$(echo "$ISSUE_BODY" | grep -A 2 'Datastore ID' | tail -n 1 | xargs)
          DISK_SIZE=$(echo "$ISSUE_BODY" | grep -A 2 'Disk Size' | tail -n 1 | xargs)
          CLONE_VM_ID=$(echo "$ISSUE_BODY" | grep -A 2 'Clone VM ID' | tail -n 1 | xargs)
          DESCRIPTION=$(echo "$ISSUE_BODY" | grep -A 2 'Description' | tail -n 1 | xargs)
          GATEWAY=$(echo "$ISSUE_BODY" | grep -A 2 'Gateway' | tail -n 1 | xargs)

          # Determine user_data_file_id
          if [ "$NODE_NAME" == "prox" ]; then
            USER_DATA_FILE_ID="proxmox_virtual_environment_file.cloud_config.id"
          else
            USER_DATA_FILE_ID="proxmox_virtual_environment_file.cloud_config2.id"
          fi

          echo "VM_NAME=$VM_NAME" >> $GITHUB_ENV
          echo "VM_ID=$VM_ID" >> $GITHUB_ENV
          echo "RAM=$RAM" >> $GITHUB_ENV
          echo "CPUS=$CPUS" >> $GITHUB_ENV
          echo "SCPU_TYPE=$SCPU_TYPE" >> $GITHUB_ENV
          echo "NODE_NAME=$NODE_NAME" >> $GITHUB_ENV
          echo "VLAN_ID=$VLAN_ID" >> $GITHUB_ENV
          echo "IP_METHOD=$IP_METHOD" >> $GITHUB_ENV
          echo "DATASTORE=$DATASTORE" >> $GITHUB_ENV
          echo "DISK_SIZE=$DISK_SIZE" >> $GITHUB_ENV
          echo "CLONE_VM_ID=$CLONE_VM_ID" >> $GITHUB_ENV
          echo "USER_DATA_FILE_ID=$USER_DATA_FILE_ID" >> $GITHUB_ENV
          echo "DESCRIPTION=$DESCRIPTION" >> $GITHUB_ENV
          echo "GATEWAY=$GATEWAY" >> $GITHUB_ENV

      - name: Generate Terraform File
        run: |
          cat <<EOF > terraform/proxmox/$VM_NAME.tf
          resource "proxmox_virtual_environment_vm" "$VM_NAME" {
              node_name    = "${{ env.NODE_NAME }}"
              vm_id        = "${{ env.VM_ID }}"
              name         = "${{ env.VM_NAME }}"
              description  = "${{ env.DESCRIPTION }}"

              cpu {
                  cores = "${{ env.CPUS }}"
                  type  = "${{ env.SCPU_TYPE }}"
                  architecture = "x86_64"
              }

              memory {
                  dedicated = "${{ env.RAM }}"
              }

              network_device {
                  bridge  = "vmbr0"
                  vlan_id = ${{ env.VLAN_ID }}
              }

              disk {
                  datastore_id = "${{ env.DATASTORE }}"
                  size         = "${{ env.DISK_SIZE }}"
                  interface    = "scsi0"
              }

              clone {
                  vm_id = "${{ env.CLONE_VM_ID }}"
              }

              initialization {
                  ip_config {
                      ipv4 {
                          address = "dhcp"
                          gateway = ${{ env.GATEWAY }}
                      }
                  }

                  user_data_file_id = ${{ env.USER_DATA_FILE_ID }}
              }

              lifecycle {
              ignore_changes = [
                initialization[0].user_account[0].keys,
                initialization[0].user_account[0].password,
                initialization[0].user_account[0].username,
                initialization[0].user_data_file_id
              ]
            }
          }
          EOF

      - name: Commit and Push Terraform File
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          git add terraform/proxmox/$VM_NAME.tf
          git commit -m "Add VM $VM_NAME configuration"
          git push


      - name: Trigger Terraform Apply Workflow
        uses: https://github.com/actions/github-script@v7
        with:
          script: |
            github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: "tofu.yml",
              ref: "main"
            })