#!/usr/bin/env node
/**
 Copyright (c) 2025 BILDIT, INC.

 This file, and the software contained herein, are the exclusive property of BILDIT, INC.
 Unauthorized copying, distribution, or modification of this software is strictly prohibited.
 All rights reserved.

 This file is licensed under the ENT License ("License"). Use of this file is subject to the
 terms and conditions specified in the License. You may obtain a copy of the License at:

     https://bildit.co/ENTLicense

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM,
 OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

;(async () => {
  const { execSync } = require('child_process')

  try {
    console.log('\n>> Checking for required feature/bugfix/hotfix/release folder in branch name.\n')
    const branchName = execSync('git symbolic-ref --short HEAD').toString().trim()
    const validBranchPattern =
      /^(develop|main|master)$|^(chore|feature|bugfix|release|hotfix)\/(BWC|BWCP|BILDIT)-\d+(-.+)?$/

    if (!validBranchPattern.test(branchName)) {
      console.error(
        `Invalid branch name: ${branchName}. Branch name must be either 'develop', 'main', 'master' or start with 'chore/', 'hotfix/', 'feature/', 'bugfix/', or 'release/' followed by a valid ticket number.`
      )
      process.exit(1)
    }
  } catch (error) {
    console.error('Error checking branch name:', error.message)
    process.exit(1)
  }
})()

